Doubt filter with linq, ASP NET MVC

2

I'm having a doubt on a filter using Linq, I have a critical object Within Critical has an ienumerable I need to return a list of critics that the status of the last moveocritica is equal to 2 But the code below is giving error

var Criticas = from a in Db.Criticas select a; 
Criticas = Criticas.Where(c => c.MovimentacoesCritica.LastOrDefault().Status.Equals(2));
    
asked by anonymous 05.03.2015 / 03:23

1 answer

2

I simulated your code in LinqPad with Lists and Enumerable and it worked correctly, probably the problem is because your query ( var Criticas = from a in Db.Criticas select a; ) returns an IQueryable object and when calling Where it can not process LastOrDefault for a new IQueryable

Try converting Critics to List or Enumerable

var Criticas = (from a in Db.Criticas select a).ToList();
    var ListCriticas = Criticas.Where(c => c.MovimentacoesCritica.LastOrDefault(o => o.Status.Equals(2)) != null);
    
05.03.2015 / 13:12