Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较两个对象集合_C#_Linq - Fatal编程技术网

C# 比较两个对象集合

C# 比较两个对象集合,c#,linq,C#,Linq,我有两个集合,一个是可用功能,另一个是用户功能。我想删除其他集合中包含featurecode但找不到正确语法的可用功能中的项目 我已经包括了我当前没有编译的代码(它抱怨我不能使用“==”操作符,我的Linq知识很少) Linq是最好的方法吗?任何帮助都将不胜感激 AvailableFeatureViewListClass availableFeatures = (AvailableFeatureViewListClass)uxAvailableList.ItemsSource;

我有两个集合,一个是可用功能,另一个是用户功能。我想删除其他集合中包含featurecode但找不到正确语法的可用功能中的项目

我已经包括了我当前没有编译的代码(它抱怨我不能使用“==”操作符,我的Linq知识很少)

Linq是最好的方法吗?任何帮助都将不胜感激

        AvailableFeatureViewListClass availableFeatures = (AvailableFeatureViewListClass)uxAvailableList.ItemsSource;
        UserFeatureListClass userFeatures = (UserFeatureListClass)uxUserFeatureList.ItemsSource;

        foreach (UserFeatureClass feature in userFeatures)
        {
            availableFeatures.Remove(availableFeatures.First(FeatureCode => FeatureCode == feature.FeatureCode));
        }

使用
Except
方法和自定义的
Equals
IEqualityComparer
实现来实现您的类型(收集项目的类型并不明显):

如果
availableFeatures
只是一组整数,您只需执行以下操作:

var features = availableFeatures.Except(userFeatures.Select(x => x.FeatureCode));

试着这样做:

var features = (from af in availableFeatures select af.FeatureCode)
            .Intersect(from uf in userFeatures select uf.FeatureCode);
这个怎么样

    IEnumerable<int> a = new List<int>() { 1, 2 };
    IEnumerable<int> b = new List<int> { 2, 3 };

    var result = a.Except(b);
    a = result;
IEnumerable a=new List(){1,2};
IEnumerable b=新列表{2,3};
var结果=a,除了(b);
a=结果;

我尝试了第二个建议,它抱怨无法推断“x”部分。听起来应该能用,但Linq现在把我甩了。@马克:你能告诉我们可用的功能视图列表类或用户功能列表类的类型是什么吗?为什么不直接使用泛型类型呢?谢谢Mehrdad,它们实际上都是BusinessObjects,但对象集合中的项共享属性(在本例中为FeatureCode)。编译器无法在标准环境中推断类型,这看起来很奇怪。但是无论如何,尝试将lambda更改为(typeofyourObjectGoesher x)=>x.featureCode谢谢Andrew,它看起来是正确的,但是当我尝试绑定时,什么都不会返回。我选中了,features变量被强制转换为IntersectIntegrator对象,有没有办法将其转换回AvailableFeatureViewList类?
    IEnumerable<int> a = new List<int>() { 1, 2 };
    IEnumerable<int> b = new List<int> { 2, 3 };

    var result = a.Except(b);
    a = result;