如何检查列表 A 不包含列表 B 中的任何元素
How to check list A not contains any elemnt from list B
我想要 return 一个列表对象,其中 productFromSqls 中任何元素的值有任何差异。
我尝试这样做但不起作用。
var result = productFromSqls.Where(
x => !productFromApis.Any(y => y.id == x.idshop))
.ToList();
示例应该 return 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"}
};
}
您需要添加一个方法来比较对象中的所有元素,这些元素在 类
中都是通用的
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 ........
}
您需要添加所有要比较的属性。
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();
我想要 return 一个列表对象,其中 productFromSqls 中任何元素的值有任何差异。 我尝试这样做但不起作用。
var result = productFromSqls.Where(
x => !productFromApis.Any(y => y.id == x.idshop))
.ToList();
示例应该 return 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"}
};
}
您需要添加一个方法来比较对象中的所有元素,这些元素在 类
中都是通用的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 ........
}
您需要添加所有要比较的属性。
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();