开发者

Querying M:M relationships using Entity Framework

开发者 https://www.devze.com 2023-04-11 12:39 出处:网络
How would I modify the following code: var result = from p in Cache.Model.Products from f in p.Flavours

How would I modify the following code:

    var result = from p in Cache.Model.Products
                 from f in p.Flavours
                 where f.FlavourID == "012541-5-5-5-651"
                 select p;

So that f.FlavourID is supplied a range of ID's as a supposed to just one value as shown in the above example?

Given the following ERD Model:

Products* => Prod开发者_StackOverflow中文版Combinations <= *Flavours

ProdCombinations is a junction/link table and simply has one composite key in there.


Of the top of my head

string [] ids = new[]{"012541-5-5-5-651", "012541-5-5-5-652", "012541-5-5-5-653"};

var result = from p in Cache.Model.Products
                 from f in p.Flavours
                 where ids.Contains(f.FlavourID)
                 select p;

There are some limitations, but an array of ids has worked for me before. I've only actually tried with SQL Server backend, and my IDs were integers.

As I understand it, Linq needs to translate your query into SQL, and it's only possible sometimes. For example it's not possible with IEnumerable<SomeClass>, which produces a runtime error, but possible with a collection of simple types.

0

精彩评论

暂无评论...
验证码 换一张
取 消