This is a migrated thread and some comments may be shown as answers.

Getting a single entity property from collection property

1 Answer 37 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Musashi
Top achievements
Rank 1
Musashi asked on 03 Mar 2016, 06:33 PM

I have a base class with some properties including a collection of Modification objects. Modification objects just hold modification data, like who modified the record, what was changed, etc.

I have another class which is like a "summary" version of the base class. Flattens some data out.
One thing I want it to do is have a Modification property that takes the latest modification from the Modification collection.

Ex.

public class BaseEntity
{
    public int PRIMARYKEY { get; set; }
    public string NAME { get; set; }
    public IList<Modification> Mods { get; set; }
}

public class BaseEntitySummary
{
    public int PRIMARYKEY { get; set; }
    public string NAME { get; set; }
    public Modification LatestMod { get; set; }
}

public class Modification
{
    public int PRIMARYKEY { get; set; }
    public int USERFK { get; set; }
    public int ModNumber { get; set; }
    public DateTime Date { get; set; }
}

I work with the BaseEntitySummary class, treating it like an entity. I have a mapping mechanism that maps BaseEntity to BaseEntitySummary, so in the end, what DataAccess sees from this:

context.BaseEntitySummarys.Where(bes => bes.LatestMod.Date < DateTime.Now)

is actually

context.BaseEntitys.Where(be => be.Mods.OrderByDescending(mod => mod.ModNumber).FirstOrDefault().Date < DateTime.Now)
    .Select(be => new BaseEntitySummary
    {
        PRIMARYKEY = be.PRIMARYKEY,
        NAME = be.NAME,
        LatestMod = be.OrderByDescending(mod => mod.ModNumber).FirstOrDefault()
    })

 

My problem lies in

context.BaseEntitys.Where(be => be.Mods.OrderByDescending(mod => mod.ModNumber).FirstOrDefault().Date < DateTime.Now)

I get a exception stating "Execution of 'System.Linq.Enumerable:FirstOrDefault(IEnumerable`1)' on the database server side currently not implemented."

 

Does anyone have any good ideas for making what I want happen? i.e. a property in an entity that takes one object from a collection property?

1 Answer, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 08 Mar 2016, 09:48 AM
Hello Musashi,

The error you are experiencing is expected. Due the complexity of such a query that is related to the specifics of the scenario, we designed the API to support it with two separate calls to the database. Like this:
var mod = context.Modifications.OrderByDescending(mod => mod.ModNumber)
    .FirstOrDefault(mod => mod.Date < DateTime.Now);
var baseEntity = mod.BaseEntity;
var baseEntitySummary = new baseEntitySummary
{
    PRIMARYKEY = baseEntity.PRIMARYKEY,
    NAME = baseEntity.NAME,
    LatestMod = mod
};

Note that in order to achieve the necessary result, you need the BaseEntity navigation property in the Modification entity.

I hope this helps.



Regards,
Doroteya
Telerik
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Tags
LINQ (LINQ specific questions)
Asked by
Musashi
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Share this question
or