简体   繁体   中英

Entity Framework One to Many Query

一对多关系

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?

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)))

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