C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#でLINQおよびLambdaとの結合を利用するにはどうすればよいですか?


内部結合は、両方のテーブルに一致するか、両方のテーブルに存在するレコードまたは行のみを返します。以下に示すように、条件に基づいて複数のテーブルへの結合を申請することもできます。複数の条件での結合を申請する必要がある場合は、匿名タイプを使用してください。

以下の例では、LinqHereに参加するために使用できる2つの方法を記述しました。部門と従業員が参加します

class Program{
   static void Main(string[] args){
      var result =
      Employee.GetAllEmployees().Join(Department.GetAllDepartments(),
      e => e.DepartmentID,
      d => d.ID, (employee, department) => new{
         EmployeeName = employee.Name,
         DepartmentName = department.Name
      });
      foreach (var employee in result){
         Console.WriteLine(employee.EmployeeName + "\t" +
         employee.DepartmentName);
      }
      var result1 = from e in Employee.GetAllEmployees()
      join d in Department.GetAllDepartments()
      on e.DepartmentID equals d.ID
      select new{
         EmployeeName = e.Name,
         DepartmentName = d.Name
      };
      foreach (var employee in result1){
         Console.WriteLine(employee.EmployeeName + "\t" +
         employee.DepartmentName);
      }
      Console.ReadLine();
   }
}
public class Employee{
   public int ID { get; set; }
   public string Name { get; set; }
   public int DepartmentID { get; set; }
   public static List<Employee> GetAllEmployees(){
      return new List<Employee>(){
         new Employee { ID = 1, Name = "A", DepartmentID = 1 },
         new Employee { ID = 2, Name = "B", DepartmentID = 2 },
         new Employee { ID = 3, Name = "B", DepartmentID = 1 },
         new Employee { ID = 4, Name = "V", DepartmentID = 1 },
         new Employee { ID = 5, Name = "F", DepartmentID = 2 },
         new Employee { ID = 6, Name = "R", DepartmentID = 2 },
         new Employee { ID = 7, Name = "TT", DepartmentID = 1 },
         new Employee { ID = 8, Name = "YY", DepartmentID = 1 },
         new Employee { ID = 9, Name = "WW", DepartmentID = 2 },
         new Employee { ID = 10, Name = "QQ"}
      };
   }
}
public class Department{
   public int ID { get; set; }
   public string Name { get; set; }
   public static List<Department> GetAllDepartments(){
      return new List<Department>(){
         new Department { ID = 1, Name = "IT"},
         new Department { ID = 2, Name = "HR"},
         new Department { ID = 3, Name = "Contract"},
      };
   }
}

出力

A IT
B HR
B IT
V IT
F HR
R HR
TT IT
YY IT
WW HR
A IT
B HR
B IT
V IT
F HR
R HR
TT IT
YY IT
WW HR

  1. 「Apple でサインイン」とは何か、その使用方法、安全性について

    WWDC 2019 で、Apple は参加者と視聴者に、すべてのデバイスで展開される新しい「Apple でサインイン」機能を紹介しました。この機能は必ずしも画期的なものでも、MacBook や iPhone を手に入れるためのセールス ポイントでもありませんが、Apple の一連のデバイス機能の一部として高く評価されるようになりました。 Apple Pay は 5 年前に開始されたもので、おそらく最も関連性が高く、同等の機能です。 Apple Pay と「Apple でサインイン」はどちらも、Apple がハードウェアとオペレーティング システムの観点からだけでなく、サードパーティ製アプリケ

  2. Bing と Qmee で検索してオンラインでお金を稼ぐ方法

    オンラインでお金を稼ぐこと自体は簡単ですが、難しいのは、できる場所を見つけることです。 お金を稼ぐ。幸いなことに、Bing を使用しているユーザーは、オンラインでお金を稼ぐ場所がすぐ目の前にあります。Bing です。かつて平凡な検索エンジンと見なされていたものが、今では非常に強力なものになっています。 Microsoft による絶え間ない改善により、Bing の結果は継続的に改善されていますが、それでも快適なエクスペリエンスが提供されています。現在、Bing を使用してお金を稼ぐことができます。 Bing Rewards (Bing を使用するユーザーに報酬を与えるための Microsoft