0

Possible Duplicate:
Entity Framework: There is already an open DataReader associated with this Command

I have these classes:

    public class VwSelectBrochures
{
    public List<SelectBrochure> SelectBrochures { get; set; }
}

   public class SelectBrochure
{
    public int BrochureId { get; set; }
    public string UrlImage { get; set; }
    public string Description { get; set; }
    public string Cat1Description { get; set; }
    public string Cat2Description { get; set; }
    public string Cat3Description { get; set; }
    public List<LangSelection> Languages { get; set; }
}

public class LangSelection
{
    public int Id { get; set; }
    public string Description { get; set; }
    public bool Vink { get; set; }
}

and this line of code:

var dbmodel2 =
from x in
    brochures.AsEnumerable().Select(
        x => new SelectBrochure {BrochureId = x.Id, Description = x.Description, UrlImage = x.UrlImage,
        Languages = new List<LangSelection>(from y in x.BrochureLanguages select new LangSelection(){Description = y.Language.Description, Id = y.Language.Id})})
select x;

Brochures and BrochureLanguages are my db-models.

I know I get the error, because I can't do the "from y in x.BrochureLanguages", but I don't see how I can fix this.

What I really want is to get all the brochures into the VwSelectBrochures class.

Community
  • 1
  • 1
jurgen_be
  • 105
  • 1
  • 1
  • 5
  • Is there a reason you can't declare and build the List before this statement and then just issue a .ToArray().ToList() on that list for each new SelectBrochure? **For that matter you could declare it as an array up above and just issue a .ToList() on that array for every new object. – Mike Perrenoud Jun 20 '12 at 11:30

3 Answers3

2

There is too little information to recommend an architectural fix, but a workaround would be to enable MARS in your connection string.

usr
  • 168,620
  • 35
  • 240
  • 369
1

The easiest way is to bring the whole brochures into the memory (using ToList()) first:

var dbmodel2 =
from x in
    brochures.ToList().Select(
        x => new SelectBrochure {BrochureId = x.Id, Description = x.Description, UrlImage = x.UrlImage,
        Languages = new List<LangSelection>(from y in x.BrochureLanguages select new LangSelection(){Description = y.Language.Description, Id = y.Language.Id})})
select x;

This way when running from y in ... there is no other DataReader open. I think there is another option using a join.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
0

I am a little unsure of query expressions and personally prefer to use the underlying extension methods to obtain results. So I am sorry this answer is not in query expression format, but could you try it?

    var dbmodel2 = brochures.AsEnumerable().Select(x => new SelectBrochure {
        BrochureId = x.Id, 
        Description = x.Description, 
        UrlImage = x.UrlImage,
        Languages = x.BrochureLanguages.Select(y => new LangSelection() {
            Description = y.Language.Description, 
            Id = y.Language.Id
        })
    }).ToList(); // this executes the whole query

Again, not being 100% sure about the query expression format, I felt the outer "from x in ... select x" was superfluous and wondered if that may have caused the error.

Joshua
  • 4,099
  • 25
  • 37
  • Simple adaptation worked flawlessly: adding ToList after "brochures." and same after y.Language.ID}).ToList() – jurgen_be Jun 20 '12 at 12:05