0

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.

1
  • What is the error? What is the result? Did you test your query? Commented Feb 23, 2017 at 6:14

1 Answer 1

1
queryable = queryable.Where(x => x.Name.Contains(searchtext) || 
                                 x.ProductInfos.Any(y => y.Name.Contains(Seachtext));
Sign up to request clarification or add additional context in comments.

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.