2011-06-26 25 views
3

包含我有類「估計」,這個類有一個屬性「EstimationItems」(類型爲IList的)使用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

使用該:

List<EstimationItem> items = new List<EstimationItem>(); 
// Add items 

int searchedCode = 1 

if(items.Any(i => i.Product.Code == searchedCode)) 
{ 
    // Contained 
} 
0
var query = from ei in EstimationItems where ei.Product.Code == "MyCode" select ei; 
1

Enumerable.Any Method確定序列中的任一元素是否滿足條件。

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

您可以使用Any()

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