Reputation: 3404
I have 'Property' objects with 'BayOption' child objects. I need the site search to search through the child BayOptions as well as the Properties and return Properties if the search criteria matches the child or parent. A Property can have multiple BayOptions (and usually does). I was unsure if .Select or .SelectMany is the trick I need. I have the following so far:
var stringResults = db.Properties
.Where(x => x.Address.Contains(id)... (more conditions here...but then BayOptions)
|| x.BayOptions.Select(g => g.Description).Contains(id)
);
But would the .Select here only select one BayOption? (I don't think so, but .SelectMany makes me wonder...) At any rate, I am not getting results for the child objects with this as it is.
Upvotes: 2
Views: 139
Reputation: 13662
One option would be to use LINQ .Any()
:
var stringResults = db.Properties.Where(x =>
x.Address.Contains(id) ||
/* Other conditions || */
x.BayOptions.Any(g => g.Description.Contains(id)));
Here, x.BayOptions.Any(g => g.Description.Contains(id))
will return true if any of the BayOptions
values have a description which contains the ID.
Upvotes: 3