我有一个集合,我需要找到一个价格最低的项目,如果超过1发现默认情况下任何应该选择,它的isPriceSelected属性需要设置为false.
我正在尝试这样的事情.
lstBtn.Where(p => p.CategoryID == btnObj.CategoryID &&
p.IsSelected == true && p.IsPriceApplied == true)
.ToList()
.Min(m=>m.Price)
Run Code Online (Sandbox Code Playgroud)
只需选择您想要的最小属性:
var minimumPrice = lstBtn
.Where(p => p.CategoryID == btnObj.CategoryID && p.IsSelected && p.IsPriceApplied)
.Min(p => p.Price);
Run Code Online (Sandbox Code Playgroud)
如果您确实想要找到价格最低的物品,您需要订购该物品:
var itemWithMinimumPrice = lstBtn
.OrderBy(p => p.Price)
.FirstOrDefault(p => p.CategoryID == btnObj.CategoryID && p.IsSelected && p.IsPriceApplied);
Run Code Online (Sandbox Code Playgroud)
或者,这可能更有效:
var itemWithMinimumPrice = lstBtn
.Where(p => p.CategoryID == btnObj.CategoryID && p.IsSelected && p.IsPriceApplied)
.OrderBy(p => p.Price)
.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
Enumerable.FirstOrDefault返回一个项目,或者null没有项目与谓词匹配.