Lost_In_Library
Lost_In_Library

Reputation: 3493

Entity Framework Search On Entity Property and Entity ICollection

I'm using entity framework 6 with C#.

My tables are like;

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

I want to search text in Product.Name and Product.ProductInfos -> Name.
Like;


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

However as you can see my brain has been stopped :)
How can query a class's property and child classes properties ?

P.s. This is not big tables, don't worry about performance errors. I have only 50 products.

Upvotes: 0

Views: 77

Answers (1)

Teppic
Teppic

Reputation: 2546

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

Upvotes: 1

Related Questions