Dynamic LINQ OrderBy Date only on DateTime Object Entity Framework

c# entity-framework entity-framework-6 linq linq-to-entities

Question

I'm now using the dynamic search capability of the Flexible LINQ (original link to codeplex) library. I must fulfil a criterion, soOrderBy a DateTime? field just considering the Date and ignoring the time. I'm using SQL Azure to query a database.EntityFramework .

The example entity is shown here.

public class SampleEntity
{
    public DateTime? DateCreated { get; set; }
    public bool Flag { get; set; }
}

The database query code is provided below.

var orderByString = "DateCreated.Value.Date asc, Flag"; //This is a dynamic string
var query = _context.Set<SampleEntity>().OrderBy(orderByString);

The parsing of this phrase ("DateCreated.Value.Date") bySystem.Linq.Dynamic It does, however, throw the following error, which is reasonable given that LINQ to Entities does not support it. Remember that I need to conduct the sorting on the server, therefore this has to function on an IQueryable (I cannot apply the solution from this thread.).

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

Utilizing is the answer isDbFunctions.TruncateTime() as said by in this response and other people. However, that won't be effective withSystem.Linq.Dynamic

Any suggestions on how to solve this issue? A potential workaround is to create a new column to the database that contains just the Date portion ofDateCreated search that column, too. I was seeking for another way to solve this issue since I'd prefer not to do it. Another strategy is to dynamically build the lambda expression and return the DbFunctions. TruncateTime, and then put it into action on the database. Any suggestions are welcome.

Thanks,

1
1
5/23/2017 11:55:16 AM

Accepted Answer

ZZZ_tmp
5
5/23/2017 10:31:03 AM


Related Questions





Related

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