C# LINQ:点符号等效于 JOIN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1511833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ: dot notation equivalent for JOIN
提问by p.campbell
Consider this LINQ expression written using query notation:
考虑使用查询符号编写的这个 LINQ 表达式:
List<Person> pr = (from p in db.Persons
join e in db.PersonExceptions
on p.ID equals e.PersonID
where e.CreatedOn >= fromDate
orderby e.CreatedOn descending
select p)
.ToList();
Question:how would you write this LINQ expression using dot notation?
问题:您将如何使用点表示法编写这个 LINQ 表达式?
采纳答案by Jon Skeet
Like this:
像这样:
List<Person> pr = db.Persons
.Join(db.PersonExceptions,
p => p.ID,
e => e.PersonID,
(p, e) => new { p, e })
.Where(z => z.e.CreatedOn >= fromDate)
.OrderByDescending(z => z.e.CreatedOn)
.Select(z => z.p)
.ToList();
Note how a new anonymous type is introduced to carry both the p
and e
bits forward. In the specification, query operators which do this use transparent identifiersto indicate the behaviour.
请注意如何引入一个新的匿名类型来向前携带p
和e
位。在规范中,执行此操作的查询运算符使用透明标识符来指示行为。