我有一个"情侣"具有以下结构
public class Couple
{
public string Code;
public string Label;
}
另一个" Stuff"像那样
public class Stuff
{
public string CodeStuff;
public string LabelStuff;
public int foo;
public int bar;
//etc...
}
我希望得到所有Stuff对象,其中每个情侣(代码,标签)在我的List<Couple>
中与(CodeStuff,Label,Stuff)匹配List<Stuff>
。
例如情侣
Couple c = new Couple { Code = "ABC", Label = "MyLabel1" };
将与
中的仅第一行匹配 Stuff s1 = new { CodeStuff = "ABC", LabelStuff = "MyLabel1" }; // OK
Stuff s1 = new { CodeStuff = "ABC", LabelStuff = "MyLabel2" }; // NOT OK
Stuff s1 = new { CodeStuff = "DEF", LabelStuff = "MyLabel1" }; // NOT OK
我尝试使用.Where
条款或.Foreach
,但我不知道如何将这对夫妇(代码,标签)放在一起。你能救我吗?
答案 0 :(得分:3)
您需要使用linq join。
List<Couple> lstcouple = new List<Couple>();
List<Stuff> lstStuff = new List<Stuff>();
var result = (from s in lstStuff
join c in lstcouple on new { CS = s.CodeStuff, LS = s.LabelStuff } equals new { CS = c.Code, LS = c.Label }
select s).ToList();
答案 1 :(得分:0)
你试过加入吗?可能需要调整,但作为一个想法:
var joined = from c in couplesList
join s in stuffList on c.Code equals s.CodeStuff
where c.Label == s.LabelStuff
select s;
答案 2 :(得分:0)
试试这个:
List<Stuff> stuffs = new List<Stuff>{s1,s2,s3};
List<Couple> couples = new List<Couple>{c};
var filteredList = stuffs.
Where
(x=> couples.Any(y => y.Code == x.CodeStuff)
&& couples.Any(y=> y.Label == x.LabelStuff)
).ToList();