2

Assuming I have an Entity Framework 4.2 class like this:

class Company
{
    public int ID { get; set; }
    public ICollection<Employee> Employees { get; set; }
}

And I have collection like this:

public ICollection<Company> Companies { get; set; }

What's an efficient way to build a list of all employees in the entire Companies collection? I do not need to worry about duplicates.

Note: I'm trying to do it without an Entity Framework context--just using the Companies collection. However, if this entails a huge performance hit, I could get a context if necessary.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • That's pretty dogmatic. Did you have a reason? Many parts of an ASP.MVC application do not have access to a context, and there is value to have the object able to perform this query without one. – Jonathan Wood Feb 13 '12 at 05:44
  • because I think this idea just creates a many-to -many relation between these tow tables!!! – Ali Foroughi Feb 13 '12 at 06:38
  • Don't use EF 4.2 tag. There was no difference between 4.1 and 4.2 and there is no reason to have this single question with its own tag which will be later on deleted automatically. – Ladislav Mrnka Feb 18 '12 at 09:03

1 Answers1

0

I'm not sure what you're trying to achieve, but to query the data you'll want to use the DbContext.

You can configure you context to automatically load the related entities by setting LazyLoadingEnabled to true, or explicitly, i.e. when querying the data, include the path to the employees, using the Include extension provided with EF > 4.0.

As soon as all companies are there you could linq it using the SelectMany method. I haven't checked, but AFAIK, LINQ to SQL will execute the query as one bunch on demand.

If you insist not to expose your DbContext or if you use a different underlying context, I would suggest you to wrap it up in a repository that does the SelectMany behind the since, but your friend are Include and SelectMany.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • In a properly constructed ASP.NET MVC application, sections of the code won't have access to the context (or repository). In these cases, it can be very handy if the entities can query content without a reference to the context. I'm adding properties and methods to a generated entity class (via a partial class). I query members without a context all the time. But I don't know how/if there's a way to construct a union of all child elements in a collection The entity class can simply query an existing collection. – Jonathan Wood Feb 13 '12 at 07:26
  • 1
    Again, if the collections are materialized, simple use the `SelectMany` functions. How do you query your data anyway? Is it using ADO.NET Linq commands? SPROCS? Is the scalability that big that you don't want to use built in support? Hard-coded SQL is very hard to maintain and is error prone and should be usually avoided. I think that's the reason LINQ to SQL was made for, any way if you're going the hard-coded way use SPROCS and have EF know about them. See [this](http://goo.gl/28dlq) or [this](http://bit.ly/zs8dX6) for more. – Shimmy Weitzhandler Feb 13 '12 at 07:38
  • Thanks. `SelectMany()` was what I was looking for. To query the data, I'm using EF LINQ queries. So far, I've been able to do stuff like this and, if another trip to the database was needed, it happened automatically. (No one is saying anything about hard-coded SQL.) – Jonathan Wood Feb 13 '12 at 17:27