码迷,mamicode.com
首页 > 其他好文 > 详细

Linq.join_lambda知识点

时间:2019-08-10 09:56:55      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:class   cell   --   equal   ref   database   eth   inner   starting   

1、一直  不太明白 join的 lambda的写法,先记录一些我查到的东西:

 1.1、有关 Linq 与 Lambda 的Join连接查询_已解决_博问_博客园.html(https://q.cnblogs.com/q/106310/

  ZC:里面有人说 “lambda 太长 比linq 恶心多了  短的时候用lambda  长的时候用linq”

 1.2、c# - Join_Where with LINQ and Lambda - Stack Overflow.html(https://stackoverflow.com/questions/2767709/join-where-with-linq-and-lambda

  ZC:里面的 被接受的回复:(ZC:可以对比一下 回复者的2中写法)

   I find that if you‘re familiar with SQL syntax, using the LINQ query syntax is much clearer, more natural, and makes it easier to spot errors:

   var id = 1;
   var query =
      from post in database.Posts
      join meta in database.Post_Metas on post.ID equals meta.Post_ID
      where post.ID == id
      select new { Post = post, Meta = meta };

   If you‘re really stuck on using lambdas though, your syntax is quite a bit off. Here‘s the same query, using the LINQ extension methods:

   var id = 1;
   var query = database.Posts    // your starting point - table in the "from" statement
      .Join(database.Post_Metas, // the source table of the inner join
         post => post.ID,        // Select the primary key (the first part of the "on" clause in an sql "join" statement)
         meta => meta.Post_ID,   // Select the foreign key (the second part of the "on" clause)
         (post, meta) => new { Post = post, Meta = meta }) // selection
      .Where(postAndMeta => postAndMeta.Post.ID == id);    // where statement

 

2、

3、

4、

5、

 

Linq.join_lambda知识点

标签:class   cell   --   equal   ref   database   eth   inner   starting   

原文地址:https://www.cnblogs.com/csskill/p/11330462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!