栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

与LINQ和Lambda一起/在哪里

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

与LINQ和Lambda一起/在哪里

我发现,如果您熟悉SQL语法,则使用LINQ查询语法会更清晰,更自然,并且更容易发现错误:

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 };

但是,如果您真的很喜欢使用lambda,那么您的语法就会有些偏离。这是使用LINQ扩展方法的相同查询:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号