0

I have the following :

class CarInspection
{
    public CarInspectionAgenda Agenda {get;set;}
    public string Description {get;set;}
    ...
}

class CarInspectionAgenda 
{
    public IList<DummyA> DummyAList {get;set;} 
    public string Name {get;set;}
    ...    
}

class DummyAList 
{
    public DummyB DummyB {get;set;}
    ...
}

class DummyB 
{
    public string Name {get;set;}
   ...
}

Ok, so in my CarInspection View, I created a CarInspectionViewModel. I need to show a custom formatted string using the DummyB Names ...

So, I created that ViewModel :

public CarInspectionViewModel
{
    public string Description {get;set;}
    public CarInspectionAgendaViewModel Agenda {get;set;}

}

public CarInspectionAgendaViewModel
{
    public string Name {get;set;}
    public IList<string> DummyANameList {get;set;} 
}

I tried something like that :

var list = carInspectionRepository.GetAll().Select(x => new CarInspectionViewModel()
{
     Id = x.Id,
     Description = x.Description,
     Agenda = new AgendaFiscalizacaoVM
     {
          Id = x.CarInspectionAgenda.Id,
          Name = x.CarInspectionAgenda.Name,
          DummyANameList = x.CarInspectionAgenda.DummyAList.Select(y => y.DummyB.Name)
     }
}); 

I got some weird results in DummyANameList ... I think that LINQ have something wrong... Any help?

Thanks

Paul
  • 12,359
  • 20
  • 64
  • 101
  • Doesn't answer your question directly, but have you thought of using Auto Mapper library. It is designed to solve exactly that kind of problem. Available in nuget or from here: https://github.com/AutoMapper/AutoMapper – Jack Hughes Nov 29 '13 at 16:25
  • I'm using ValueInjecter... But I'll have the same problem using any of then in that casa I think... I'll have the create a custom mapping and then do the same thing... – Paul Nov 29 '13 at 16:31
  • What kind of weird results? can you see the generated sql? – MikeSW Nov 29 '13 at 17:33
  • Repeated values in DummyANameList... – Paul Nov 29 '13 at 20:49
  • If you are using eager fetching, it may cause the problem. See http://stackoverflow.com/q/10320328/1236044 – jbl Dec 02 '13 at 09:03

1 Answers1

0

Short answer:

You are missing .ToList() in the LINQ statements.

var list = carInspectionRepository.GetAll().Select(x => new CarInspectionViewModel()
{
    Id = x.Id,
    Description = x.Description,
    Agenda = new AgendaFiscalizacaoVM
    {
        Id = x.CarInspectionAgenda.Id,
        Name = x.CarInspectionAgenda.Name,
        DummyANameList = x.CarInspectionAgenda
            .DummyAList
            .Select(y => y.DummyB.Name)
            .ToList()
    }
}).ToList(); 


Longer answer:

LINQ statements (in most cases) are not evaluated immediately. Evaluation is postponed until somewhere later in your code variable created by LINQ is used. If statement cannot be evaluated successfully, LINQ (again in most cases) will throw exception in the line where you are using such variable. Calling ToList() or ToArray() will evaluate LINQ statement immediately.

I suppose (because I don't see full code) that DummyAList is not loaded immediately from database (e.g. LazyLoad in NHibernate) and the place where you are using result list is outside of the scope of database session, so LINQ evaluation fails.

mozgow
  • 197
  • 1
  • 13
  • I think its no the case, because i got the result fine... But there is several repeated values in DummyANameList... For example, In cases where there is just one element in DummyAList, Im getting 3 repeated strings. – Paul Nov 29 '13 at 20:49
  • @Paul To my knowledge it's not possible to get 3 elements by using simple `Select` on one element collection. Are you certain that result list is not modified somewhere else? – mozgow Nov 29 '13 at 20:56