LINQ表达式从2列表中获得一个属性的差值

本文关键字:一个 属性 表达式 列表 LINQ | 更新日期: 2023-09-27 18:06:31

我有两个属性列表

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;

i want to get a new list

List<Lookup> changes; 

其中changes包含来自current的物品,且history中物品的价格属性存在差异。是否有像JOININTERSECT这样的LINQ表达式来过滤它?还是我需要手动遍历所有项?如果是JOIN,我怎么能在ItemIdStoreID两个属性上做到这一点(这两个属性的组合是唯一的)。

LINQ表达式从2列表中获得一个属性的差值

你是这个意思?

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<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();