我必须在两个列表中找到巧合,一个是特定实体,另一个是元组
实体很简单,就像这样:
public class SUBJECT
{
int id;
int group;
}
巧合必须与SUBJECT实体的两个属性一起,并且元组的两个值,SUBJECT.group的列表是必需的,我写了这段代码,但我猜有更好的方法这样做
List<SUBJECT> subjects;
List<Tuple<int, int>> groups;
List<int> intersected = new List<int>();
foreach (Tuple<int, int> g in groups)
{
List<int> found = subjects
.Where(x => x.id == item.Item1 && x.group == item.Item2)
.Select(x => x.group).ToList();
if (found != null && found.Any())
intersected.AddRange(found);
}
return intersected;
由于
答案 0 :(得分:1)
首先,我将主题转换为Tuples
,然后使用Intersect
:
var intersected = subjects
.Select(x => Tuple.Create(x.id, x.group))
.Intersect(groups)
.Select(x => x.group)
.ToList();
这样可行,因为Tuples
会根据属性值进行比较。