C# LINQ表达式用于从具有属性的2列表中获取差异
我有两个属性列表C# LINQ表达式用于从具有属性的2列表中获取差异,c#,wpf,linq,C#,Wpf,Linq,我有两个属性列表 public class Lookup { public int SlNo{get;set;} public int StoreId{get;set;} public int ItemId{get;set;} public string Name{get;set;} public int Price{get;set;} } List<Lookup> current; List<Lookup> history; 公共类查找 { 公共int SlNo{g
public class Lookup
{
public int SlNo{get;set;}
public int StoreId{get;set;}
public int ItemId{get;set;}
public string Name{get;set;}
public int Price{get;set;}
}
List<Lookup> current;
List<Lookup> history;
公共类查找
{
公共int SlNo{get;set;}
public int StoreId{get;set;}
公共int ItemId{get;set;}
公共字符串名称{get;set;}
公共整数价格{get;set;}
}
列示电流;
列出历史记录;
我想要一份新的名单
List<Lookup> changes;
列表更改;
其中changes
包含current
中的项目,且历史记录中的项目具有不同的价格属性。
是否有像JOIN
或INTERSECT
这样的LINQ表达式来过滤掉它?
或者我需要手动迭代所有项目吗?
如果是JOIN
我如何在两个属性ItemId
和StoreID
(这两个属性的组合是唯一的)。你的意思是
changes = (from c in current
join h in history on c.Id equals h.Id
where c.Price != h.Price &&
c.StoreId == h.StoreId
select c).ToList();
List current=新列表();
列表历史=新列表();
列表更改=新列表();
添加(新的查找(){Id=1,Name=“sdf”,Price=20});
添加(新的查找(){Id=2,Name=“asdf”,Price=30});
添加(新的查找(){Id=3,Name=“bsdf”,Price=40});
Add(newlookup(){Id=1,Name=“sdf”,Price=10});
Add(newlookup(){Id=2,Name=“asdf”,Price=30});
Add(newlookup(){Id=3,Name=“bsdf”,Price=10});
更改=(从当前的cur加入his历史记录)
cur.Id等于他的.Id,其中cur.Price!=他的.Price选择
新建查找(){Id=his.Id,Name=his.Name,Price=his.Price})。
托利斯特();
是的,他们被称为(喘息)和…不知道为什么这个问题得到了大量的赞成票。通过在线搜索,你可以在几秒钟内找到一个清晰的答案。@GlenThomas我希望你能看到有问题的编辑。由于我将属性组合作为唯一键,所以很难获取带有更改的项。如果你能分享一些研究链接,那就太好了。我怎么能在“内部联接”中提到2个属性呢。因为在我的情况下,ID不是主键,我不能在join中提到ID=ID,因为它们无论如何都不会相同,那么连接条件是什么?你说了两个属性,现在你说Id
不一样了,那么如果它们不一样,它怎么可能成为连接的一部分呢?StoreID和ItemID是唯一的组合。对于历史_列表,Id基本上是数据库Id;对于当前_列表,Slno仅[始终启动wuth 1]
List<Lookup> current = new List<Lookup>();
List<Lookup> history = new List<Lookup>();
List<Lookup> changes= new List<Lookup>();
current.Add(new Lookup() { Id = 1, Name = "sdf", Price = 20 });
current.Add(new Lookup() { Id = 2, Name = "asdf", Price = 30 });
current.Add(new Lookup() { Id = 3, Name = "bsdf", Price = 40 });
history.Add(new Lookup() { Id = 1, Name = "sdf", Price = 10 });
history.Add(new Lookup() { Id = 2, Name = "asdf", Price = 30 });
history.Add(new Lookup() { Id = 3, Name = "bsdf", Price = 10 });
changes = (from cur in current join his in history on
cur.Id equals his.Id where cur.Price != his.Price select
new Lookup() { Id = his.Id, Name = his.Name, Price = his.Price }).
ToList();