简体   繁体   中英

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 ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM