C# Linq 与对象的一部分相交/除外 [英] C# Linq intersect/except with one part of object
本文介绍了C# Linq 与对象的一部分相交/除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堂课:
class ThisClass
{
private string a {get; set;}
private string b {get; set;}
}
我想使用Linq的Intersect和Except方法,即:
I would like to use the Intersect and Except methods of Linq, i.e.:
private List<ThisClass> foo = new List<ThisClass>();
private List<ThisClass> bar = new List<ThisClass>();
然后我分别填写两个列表.例如,我想做(我知道这是不对的,只是伪代码),如下:
Then I fill the two lists separately. I'd like to do, for example (and I know this isn't right, just pseudo code), the following:
foo[a].Intersect(bar[a]);
我该怎么做?
推荐答案
也许
// returns list of intersecting property 'a' values
foo.Select(f => f.a).Intersect(bar.Select(b => b.a));
BTW 属性 a
应该是公开的.
BTW property a
should be public.
这篇关于C# Linq 与对象的一部分相交/除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文