Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework One to Many Query

One to Many relationship

I have this model.

Rule.

A Company can have multiple service

The Problem

User can enter a list of service name(list of string) and then can retrieve a company which has that list of service.

Question

How can i do this? The first solution that comes to my mind is to join the table first and then use the lambda expression from the joined table. Any other solution?

like image 515
donthurtme Avatar asked Aug 11 '26 00:08

donthurtme


1 Answers

You can do it with Where():

Companies.Where(c => requiredServices.All(s => c.Services.Contains(s)))

If List<string> is used then:

Companies.Where(company => serviceNames.All(
    serviceName => company.Services.Any(service => service.Service_Name == serviceName)))
like image 72
at least 3 and no more than 30 Avatar answered Aug 13 '26 12:08

at least 3 and no more than 30



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!