LINQのCancatオペレーター(9)

LINQの接続演算子には、主にJoin()とGroupJoin()が含まれます。

まず、結合()演算子

 Join()演算子は、T-SQLの内部結合と非常に似ています。2つのデータソースを接続し、2つのデータソースの等しい値に基づいて照合します。

サンプルコード:

システムの使用;
System.Collections.Generic; を使用します。
System.Linq を使用します。
System.Text を使用します。
System.Threading.Tasks を使用します。

名前空間ConnectOperation 
{ 
    public  class Category 
    { 
        public  int Id { get ; セット; }
         public  string CategoryName { get ; セット; }
         public DateTime CreateTime { get ; セット; } 
    } 

    パブリック クラス Product
    {
        public  int Id { get ; セット; }
         public  int CategoryId { get ; セット; }
         public  string Name { get ; セット; }
         public  double Price { get ; セット; }
         public DateTime CreateTime { get ; セット; } 
    } 

    クラスProgram 
    { 
        static  void Main(string [] args)
        { 
            //初始化数値データ 
            List <Category> listCategory = new List <Category> ()
            { 
              new Category(){Id = 1、CategoryName = " 计算机"、CreateTime = DateTime.Now.AddYears(-1 )}、
               new Category(){Id = 2、CategoryName = " 文学"、CreateTime = DateTime.Now.AddYears(-2 )}、
               新しい Category(){Id = 3、CategoryName = " 高校教材"、CreateTime = DateTime.Now.AddMonths(-34 )} 、
               新しい Category(){Id = 4、CategoryName = " 心理学"、CreateTime = DateTime.Now.AddMonths(-34 )} 
            }; 
            リスト <製品> listProduct = 新しいリスト<製品> ()
            { 
               製品(){ID = 1、区分= 1、NAME = " C#高级编程第10版"、=価格100.67、CREATETIME = DateTime.Now}、
                新しいです Product(){Id = 2、CategoryId = 1、Name = " Redis开発和运维"、Price =69.9、CreateTime = DateTime.Now.AddDays(-19 )}、
                新しい Product(){Id = 3、CategoryId = 2、Name = " 活着"、Price = 57、CreateTime = DateTime.Now.AddMonths(-3 )}、
                new Product(){Id = 4、CategoryId = 3、Name = " 高等数学"、Price = 97、CreateTime = DateTime.Now.AddMonths(-1 )}、
                new Product(){Id = 5、CategoryId = 6、名前= " 国家宝藏"、価格= 52.8、CreateTime = DateTime.Now.AddMonths(-1 )} 
            }; 

            // 1、查询表达式
            var queryExpress = from c in listCategory 
                               join p in listProduct on c.Id equals p.CategoryId
                                select  new {Id = c.Id、CategoryName = c.CategoryName、ProductName = p.Name、PublishTime = p .CreateTime}; 
            Console.WriteLine(" 查询表达式输出:" );
            foreachのVARの項目queryExpress)
            { 
                Console.WriteLine($ " id:{item.Id}、CategoryName:{item.CategoryName}、ProductName:{item.ProductName}、PublishTime:{item.PublishTime} " ); 
            } 

            Console.WriteLine(" メソッド语法输出:" );
            // 2、メソッド语法
            var queryFun = listCategory.Join(listProduct、c => c.Id、p => p.CategoryId、(c、p)=> new {Id = c.Id、CategoryName = c.CategoryName、 ProductName = p.Name、PublishTime = p.CreateTime});
            foreachのVARの項目queryFun)
            { 
                Console.WriteLineを($ "id:{item.Id}、CategoryName:{item.CategoryName}、ProductName:{item.ProductName}、PublishTime:{item.PublishTime} " ); 
            } 

            Console.ReadKey(); 
        } 
    } 
}
コードを表示

 

おすすめ

転載: www.cnblogs.com/LuckyZLi/p/12709215.html