views:

53

answers:

2

Hi,

If you are making a LinQ to entities expression for ClassA where A has a relation to ClassB like this:

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

You will get a set of ClassA:s with the reference to ClassB loaded. The thing in my case is that I dont really need to load ALL the ClassB-references, just a few of them. But I dont want to loop through the list of ClassA:s and load them individually, i want my database operations to be fewer and bigger instead of reading small chunks here and there.

Is it possible to put some kind of restrictions on which references to include or do you have to accept this all or nothing style?

+1  A: 

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
    };

This loads only the specified ClassBs.

Craig Stuntz
AFAIK, Include is not needed.
LukLed
Right; thanks. Cut and paste error. I'll fix.
Craig Stuntz
A: 

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) };
Luhmann