Linq To Entities, Select multiple sets of data with one query

c# entity-framework entity-framework-6 linq linq-to-entities

Question

I've found plenty of info on how to select multiple result sets with stored procedures but nothing substantial on how to do so with a linq query.

For example, I can do sub-queries that return mulitple sets of results with something like

var query = (from school in context.Schools
             where school.id == someId
             select new
             {
                 subSetA = (from student in context.Students
                            select student).ToList(),
                 subSetB = (from building in context.Buildings
                            select building).ToList(),
             }).First();

query.subSetA;  //Access subSetA
query.subSetB; //Access subSetB

Which works fine, but what if I just want to select both subSetA and subSetB without querying against the school table? I want to select two separate sets of data that gets sent to the server in one query.

Any information as to how to do this with EF 6 would be great.

1
1
5/20/2015 6:38:19 PM

Popular Answer

Well, I'm sure there are many ways to do this, but if you want to avoid introducing a third DbSet into the mix...

var query = (from s in context.Students.Take(1)
             select new
             {
                 subSetA = context.Students.ToList(),
                 subSetB = context.Buildings.ToList(),
             })

Then, you can use query.ToList() or maybe using query.Load() and working with context.Students.Local, etc. would work.

0
5/20/2015 7:15:15 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow