C# 如何检查列表A不包含列表B中的任何元素

C# 如何检查列表A不包含列表B中的任何元素,c#,list,linq,contains,C#,List,Linq,Contains,我想返回一个列表对象,其中productFromSqls中任何元素的值都有差异。 我试着这样做,但不起作用 var result = productFromSqls.Where( x => !productFromApis.Any(y => y.id == x.idshop)) .ToList(); 示例应返回ProductFromSql id=1和id=2 型号 public class ProductFromSql { p

我想返回一个列表对象,其中productFromSqls中任何元素的值都有差异。 我试着这样做,但不起作用

var result = productFromSqls.Where(
            x => !productFromApis.Any(y => y.id == x.idshop))
            .ToList();
示例应返回ProductFromSql id=1和id=2

型号

public class ProductFromSql
{
    public string idEshop { get; set; }
    public string active { get; set; }

    public string code { get; set; }
    public string amount { get; set; }
    public string description { get; set; }

}


public class ProductFromApi
{
    public string id { get; set; }
    public string active { get; set; }

    public string code { get; set; }
    public string amount { get; set; }
    public string description { get; set; }
    public string createDate { get; set; }
    public string updated_at { get; set; }
    public string  man_code  { get; set; }
}
列表

public static void NotEquals()
{

    var productFromSqls = new List<ProductFromSql> {
            new ProductFromSql {idEshop="1", active="0",code="2dw",amount="22",description="testowy opis"},
            new ProductFromSql {idEshop="2", active="1",code="kk34",amount="11",description="testowy opis 2"},
            new ProductFromSql {idEshop="3", active="0",code="2323",amount="22",description="testowy opis 3"} 
    };

    var productFromApis = new List<ProductFromApi> {
            new ProductFromApi {id="1", active="1",code="2dw",amount="22",description="testowy opis",createDate="20180312",updated_at="20170419",man_code="AA2"},
            new ProductFromApi {id="2", active="1",code="kk34",amount="33",description="testowy opis 2",createDate="20180322",updated_at="20170412",man_code="AA4"},
            new ProductFromApi {id="3", active="0",code="2323",amount="22",description="testowy opis 3",createDate="20180311",updated_at="20170402",man_code="BA4"}
    };

}
publicstaticvoidnotequals()
{
var productFromSqls=新列表{
新产品fromsql{idEshop=“1”,active=“0”,code=“2dw”,amount=“22”,description=“testowy opis”},
新产品fromsql{idEshop=“2”,active=“1”,code=“kk34”,amount=“11”,description=“testowy opis 2”},
新产品fromsql{idEshop=“3”,active=“0”,code=“2323”,amount=“22”,description=“testowy opis 3”}
};
var productfromapi=新列表{
新产品来自API{id=“1”,active=“1”,code=“2dw”,amount=“22”,description=“testowy opis”,createDate=“20180312”,更新地址为=“20170419”,man\u code=“AA2”,
新产品来自API{id=“2”,active=“1”,code=“kk34”,amount=“33”,description=“testowy opis 2”,createDate=“20180322”,更新地址为=“20170412”,man\u code=“AA4”,
新产品来自API{id=“3”,active=“0”,code=“2323”,amount=“22”,description=“testowy opis 3”,createDate=“20180311”,更新地址为=“20170402”,man\u code=“BA4”}
};
}

您需要添加一个方法,该方法将比较两个类中通用的对象中的所有元素

List<ProductFromSql> Filtered = new List<ProductFromSql>();

productFromSqls.ForEach(x => productFromApis.ForEach(ele => 
                             {
                                if(!AreObjectsEqual(x, ele) && !Filtered.Any(ele => ele.id == x.id)) 
                                    Filtered.Add(x);  
                             }));


public bool AreObjectsEqual(ProductFromSql obj1, ProductFromApi obj2)
{
   return (obj1.id == obj2.idEshop && obj1.active == obj2.active)//......) //Add other properties of your class which are common in both in ........
}
List Filtered=新列表();
productFromSqls.ForEach(x=>productFromAPI.ForEach(ele=>
{
如果(!AreObjectsEqual(x,ele)&&!Filtered.Any(ele=>ele.id==x.id))
过滤。添加(x);
}));
public bool AreObjectsEqual(ProductFromSql obj1,ProductFromApi obj2)
{
return(obj1.id==obj2.idEshop&&obj1.active==obj2.active)//……//添加类的其他属性,这两个属性在…….中都是通用的。。。。。。。。
}

您需要添加所有要比较的属性

var result = productFromSqls.Where(x => !productFromApis.Any(y => y.id == x.idEshop
                                                               && y.active == x.active
                                                               && y.code == x.code
                                                               && y.amount == x.amount
                                                               && y.description == x.description)).ToList();

您是否尝试过使用
.Except(…)
linq运算符?因为它们都是字符串,所以请将它们全部连接起来,并在try var result=productFromSqls.Where(x=>!productFromAPI.Except(y=>y.id==x.idEshop)).ToList()时检查它们是否不同;我收到错误“无法将lambda表达式转换为类型‘IEnumerable’,因为它不是委托类型”。抱歉,只有我的示例只有字符串类型。普通API返回其他类型value@derloopkat如果我移除!我获取id=idshop的所有对象,并返回3个对象。我想通过id将对象从连接到列表,并检查某些元素是否不同,然后放入新列表。下面的解决方案正是我的观点。解决方案不起作用。结果是错误的。筛选列表中有重复对象。