我有类“Estimation”,这个类有一个属性“EstimationItems”(类型是IList)
public class EstimationItem
{
public virtual int Id { get; set; }
public virtual Product Product { get; set; }
public virtual int Quantity { get; set; }
public virtual decimal Price { get; set; }
}
public class Product
{
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual string Name { get; set; }
}
当我有一个“Estimation”的实例时,我想知道“EstimationItems”是否包含代码为“MyCode”的产品。
答案 0 :(得分:6)
使用此:
List<EstimationItem> items = new List<EstimationItem>();
// Add items
int searchedCode = 1
if(items.Any(i => i.Product.Code == searchedCode))
{
// Contained
}
答案 1 :(得分:6)
您可以使用Any():
bool hasMyCode = yourEstimation.EstimationItems.Any(
item => item.Product.Code == "MyCode");
答案 2 :(得分:1)
Enumerable.Any Method确定序列的任何元素是否满足条件。
Boolean result = estimationItems.Any(x => x.Product.Code == "MyCode");
答案 3 :(得分:0)
var query = from ei in EstimationItems where ei.Product.Code == "MyCode" select ei;