LINQ Lambda: Getting Last item (where == ) of sub collection

c# entity-framework linq linq-to-entities


I have the following (which I've used in CodeFirst):

public class MyForm
        public int MyFormId { get; set; }
        //Many Form Properties
        public virtual ICollection<AuditItem> AuditItems { get; set; }

public class AuditItem
        public int AuditItemId { get; set; }
        public int MyFormId { get; set; }
        public IAppUser User { get; set; }
        public string ActionByUserStr { get; set; }
        public string ActionByUserDisplayName { get; set; }
        public DateTime DateOfAction { get; set; }
        public string TypeOfAction { get; set; }
        public int Step { get; set; }

        public virtual MyForm MyForm { get; set; }

The AuditItem tracks many actions carried out on MyForm

I'm presenting an index page showing a table where the AuditItem's last Step = 1.

I do an orderby DateOfAction (because the Step can get setback to 0, so I wouldn't want it to equal 1 unless it is also the last AuditItem Record for that MyForm), then Select the last record's step, then query on that. I want to query as early as I can so that I'm not pulling back unrequired MyForms records.

This is the query I have:

var myFormsAtStep1 =
                    mf => mf.AuditItems.OrderBy(ai => ai.DateOfAction).Last().Step == 1);

It gives this error:

LINQ to Entities does not recognize the method 'MyForms.Models.AuditItem Last[AuditItem](System.Collections.Generic.IEnumerable`1[MyForms.Models.AuditItem])' method, and this method cannot be translated into a store expression.
8/13/2012 4:09:32 PM

Accepted Answer

Have a look at the list of Supported and Unsupported LINQ Methods.

Last() is not supported, but First() is, so you can just go on and reverse your ordering and use First() instead of Last().

mf => mf.AuditItems.OrderByDescending(ai => ai.DateOfAction).First().Step == 1);
8/14/2012 7:53:50 AM

Popular Answer

You simply sort in descending order and get the first item.

var myFormsAtStep1 =
        mf => mf.AuditItems.OrderByDescending(
            ai => ai.DateOfAction).First().Step == 1);

I would also use the query syntax, which would be more readable, as well as the strongly-typed Include (import System.Data.Entity):

from mf in context.MyForms.Include(x => x.AuditItems)
let items = from ai in mf.AuditItems
            orderby ai.DateOfAction descending
            select ai;
where items.First().Step == 1
select mf

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow