LiNQ for individuals, turn less

If you create a LinQ expression for entities for ClassA, where A is related to ClassB, like this:

var temp = from p in myEntities.ClassA.Include("ClassB") where ... select p; 

You will receive a set of ClassA: s with a link to download ClassB. The thing in my case is that I really don't need to load ALL ClassB links, just some of them. But I don’t want to iterate over the ClassA: s list and load them separately, I want my database operations to be less and less, instead of reading small pieces here and there.

Is it possible to set some restrictions to which the links refer, or you have to accept this style or nothing?

+4
source share
2 answers

Yes, you should project instead of using Include :

 var from p in myEntities.ClassA where ... select new { ClassA = p, ClassBs = from q in p.ClassB where (something) select q }; 

Loads only the specified ClassB.

+1
source

You can do something like this:

 var temp = from p in myEntities.ClassA select new { ClassA = p, ClassB = p.ClassB.Where(b => b.SomeProp == somevalue) }; 
0
source

Source: https://habr.com/ru/post/1303452/


All Articles