1

Why does Entity Framework (4.1), with the data model you might expect behind the following query, return an InvalidOperationException ("There is already an open DataReader associated with this Command which must be closed first") on this?

((IEnumerable<Company>)db.Companies).SelectMany(x => x.Department).ToList();
stovroz
  • 6,835
  • 2
  • 48
  • 59

2 Answers2

2

Your query is likely not a problem. You may need to turn on the MultipleActiveResultSets attribute in your connection string. Check out the answer for this one.

Community
  • 1
  • 1
Bear In Hat
  • 1,746
  • 1
  • 10
  • 19
0

Because using Linq to EF as an IEnumerable mantains a DataReader open, thus you can't make other queries before closing it. Try materializing the collection as an IList, like this:

db.Companies.ToList().SelectMany(x => x.Department).ToList();
Felipe Castro
  • 1,623
  • 11
  • 10