所以我有三个清单:
// Members stores all non-sorted member objects
var Members = new List<Member>();
// Years stores all the expected graduation years I want to compare
var Years = new List<int>();
// Sorted will contain all of the members that have a matching year
var Sorted = new List<Member>();
我正在使用foreach循环对这些值进行排序
foreach (var m in Members)
{
foreach (var y in Years)
{
if (m.ExpectedGraduationYear == y)
{
Sorted.Add(m);
}
}
}
有没有办法将其压缩成单个linq语句?
答案 0 :(得分:1)
您可以使用此类LINQ查询:
Sorted = (from m in Members
join y in Years
on m.ExpectedGraduationYear equals y
select m).ToList();
答案 1 :(得分:1)
Members.Where(m => Years.Contains(m.ExpectedGraduationYear)).ToList()
答案 2 :(得分:1)
这样的事情:
var Sorted = (from m in Members
from y in Years
where m.ExpectedGraduationYear == y select m).ToList();
答案 3 :(得分:0)
是的,就像这样:
{{1}}
答案 4 :(得分:0)
var Sorted = (from m in Members from y in Years.Where(y => m.ExpectedGraduationYear == y)
select m).ToList();