我有这个课:

public class Change()
{
    public int Id {get; set;}
    public decimal Price {get; set;}
}


我有2个列表oldPricesnewPrices。这两个列表包含相同的项目,尽管实例不同。

newPrices中的某些商品具有不同的价格,因此要获取我正在做的更改价格的列表:

var list = newPrices
             .Where(x => oldPrices.All(p => p.Price != x.Price))
             .ToDictionary(x => x.Id, x => x.Price);


此表达式应该正确,但是即使有更改,列表也为空。

我想念什么?

最佳答案

如果所有条件均满足,则Enumerable.All返回true。那不是您要检查的。您想获得所有与旧价格不同的新价格。

您必须先按Id加入两个列表,然后才能比较价格:

var joined = from np in newPrices
             join op in oldPrices
             on np.Id equals op.Id
             where np.Price != op.Price
             select np;
var newPricesById = joined.ToDictionary(p => p.Id, p => p.Price);

关于c# - 使用All()时Lambda表达式未返回正确的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38459991/

10-13 06:06
查看更多