2011-06-26 2 views
3
私が「推定」のインスタンスを持っている場合は、私が知りたいのですが、私持っているクラス「推定」、このクラスは、プロパティを持っている「EstimationItems」(タイプはILi​​stのです)

は、LINQの

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; } 
} 

に含まれてい"EstimationItems"にコード "MyCode"の商品が含まれている場合この使用

答えて

7

0
var query = from ei in EstimationItems where ei.Product.Code == "MyCode" select ei; 
1
List<EstimationItem> items = new List<EstimationItem>(); 
// Add items 

int searchedCode = 1 

if(items.Any(i => i.Product.Code == searchedCode)) 
{ 
    // Contained 
} 

Enumerable.Any Methodシーケンスの任意の要素が条件を満たしているか否かを判定する。

Boolean result = estimationItems.Any(x => x.Product.Code == "MyCode"); 
6

あなたはAny()を使用することができます。

bool hasMyCode = yourEstimation.EstimationItems.Any(
    item => item.Product.Code == "MyCode");