Linq除了通过特定字段进行比较外
我的数据库中有Linq除了通过特定字段进行比较外,linq,asp.net-mvc-3,Linq,Asp.net Mvc 3,我的数据库中有列表A,我从外部获得了列表B。我想得到 列表A和B具有相同的属性 List<Item> A { ID : 1, Name: "Tool A" , Status : 1, Price : 100 } { ID : 2, Name: "Tool B" , Status : 2, Price : 200 } { ID : 3, Name: "Tool C" , Status : 3, Price : 300 } { ID : 4, Name: "Tool D" , Stat
列表A
,我从外部获得了列表B
。我想得到
列表A和B具有相同的属性
List<Item> A
{ ID : 1, Name: "Tool A" , Status : 1, Price : 100 }
{ ID : 2, Name: "Tool B" , Status : 2, Price : 200 }
{ ID : 3, Name: "Tool C" , Status : 3, Price : 300 }
{ ID : 4, Name: "Tool D" , Status : 4, Price : 400 }
{ ID : 5, Name: "Tool E" , Status : 5, Price : 500 }
List<Item> B
{ ID : 1, Name: null , Status : 1, Price : 100 }
{ ID : 2, Name: null , Status : 2, Price : 200 }
{ ID : 3, Name: null , Status : 3000, Price : 300 }
{ ID : 4, Name: null , Status : 4, Price : 40000 }
{ ID : 5, Name: null , Status : 5, Price : 500 }
属性“Name”的值来自列表A
,即使它们在列表B
我在想这样的事情,但它给了我一个奇怪的结果
var ChagnedList = A.AsEnumerable()
.Where(aa => B.Any(bb => aa.Status != bb.Status || aa.Price != bb.Price))
您可以在中使用ExceptBy方法:
var ChagnedList = A.AsEnumerable()
.Where(aa => B.Any(bb => aa.Status != bb.Status || aa.Price != bb.Price))
var ChagnedList = A.AsEnumerable()
.ExceptBy(B, item => new {item.Status,item.Price});