1

I have two Entity Framework objects with a one-to-many relationship:

widget(parent) and widgetNail(child)

Widget has a text column: Title WidgetNail has a text column: Description

I would like to build a query that will return a list of Widgets that match one of two criteria:

  1. A text string is found in the Widget title, or
  2. The same text string is found in any WidgetNail description.

So far I have this, which doesn't work...

from widget in entities.Widgets
from widgetNail in entities.WidgetNails
where widget.Title.Contains(searchText)
|| widgetNail.Description.Contains(searchText)
select widget).ToList();

1 Answer 1

2

Regarding

2.The same text string is found in any WidgetNail description.

You mean among the current Widget's children?

(from widget in entities.Widgets
where widget.Title.Contains(searchText) || widget.WidgetNails.Any(wn => wn.Description.Contains(searchText))
select widget).ToList();

or fluent syntax:

entities.Widgets.
         Where(w => w.Title.Contains(searchText) ||
                    w.WidgetNails.Any(wn => wn.Description.Contains(searchText))).
         ToList();
Sign up to request clarification or add additional context in comments.

Comments

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.