2017-02-23 122 views
0

我正在使用實體框架6與C#。實體框架搜索實體屬性和實體ICollection

我的表就像;

public class Product 
{ 
    public Product() 
    { 
     ProductInfos = new List<ProductInfo>(); 
    } 

    ... 

    public string Name { get; set; } 

    public virtual ICollection<ProductInfo> ProductInfos { get; set; } 
} 

public class ProductInfo 
{ 
    ... 

    public long ProductId { get; set; } 

    public string Name { get; set; } 
} 

我想在Product.NameProduct.ProductInfos搜索文本 - >Name
喜歡;


        queryable = queryable.Where(x => x.Name.Contains(searchtext)) 
             .Where(p => p.ProductInfos.Where(p => p.Name.Contains(searchtext))); 

但是,你可以看到我的大腦已經停止:)
如何查詢類的屬性和子類的屬性?

P.s.這不是大表,不用擔心表現錯誤。我只有50種產品。

+0

什麼是錯誤?結果是什麼?你測試了你的查詢嗎? – CodeNotFound

回答

1
queryable = queryable.Where(x => x.Name.Contains(searchtext) || 
           x.ProductInfos.Any(y => y.Name.Contains(Seachtext)); 
+0

雖然此代碼片段可能會解決問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 –