Linq.join_lambda知识点

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、

猜你喜欢

转载自www.cnblogs.com/csskill/p/11330462.html