Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime Add calculation inside Linq to entities query

I am trying to add datetime in Linq query.

Error is : LINQ to Entities does not recognize the method 'System.DateTime AddMinutes(Double)' method, and this method cannot be translated into a store expression.

It is working in Link to SQL but not working to LINQ to Entity.

Is there any another way to do this.

Thanks

DataClassesDataContext datacontext = new DataClassesDataContext();
        int concertid = Convert.ToInt32(ddlConcerts.SelectedValue.ToString());
        GridView1.DataSource = (from ticketallocation in datacontext.tblConcertTicketAllocation
                                where ticketallocation.ConcertID == concertid
                                && ticketallocation.Section == ddlSection.SelectedValue
                                && ticketallocation.Row == ddlRows.SelectedValue
                                select new
                                {
                                    AutoID = ticketallocation.AutoID,
                                    ConcertID = ticketallocation.ConcertID,
                                    Section = ticketallocation.Section,
                                    Row = ticketallocation.Row,
                                    Seats = ticketallocation.Seats,
                                    Status = ticketallocation.Status == 3 ? "<span style=\"color:#FF0000;\">Sold</span>" :
                                    ticketallocation.Status == 2 ? "<span style=\"color:#999999;\">Aisle Break</span>" :
                                    ticketallocation.Status == 0 ? "<span style=\"color:#009900;\">Available</span>" :
                                    ticketallocation.Status == 1 && ticketallocation.DateandTime.Value.AddMinutes(16) > DateTime.Now ? "<span style=\"color:#FF9900;\">Looking</span>" : "<span style=\"color:#009900;\">Available</span>",
                                    DateandTime = ticketallocation.DateandTime,
                                    OrderNumber = ticketallocation.OrderNumber
                                }).ToList();
        GridView1.DataBind();
like image 294
user661680 Avatar asked Mar 16 '11 01:03

user661680


2 Answers

In EntityFramework 6 EntityFunctions are gone.

Use

System.Data.Entity.DbFunctions

instead

This isn't answer to the OP question, but maybe it helps to some incoming viewers.

like image 142
Gh61 Avatar answered Oct 02 '22 00:10

Gh61


If you are using Entity Framework v4, try this code

System.Data.Objects.EntityFunctions.AddMinutes(ticketallocation.DateandTime.Value, 16) 

instead of

ticketallocation.DateandTime.Value.AddMinutes(16)

If you are using Entity Framework v1, the solution is Entity SQL Canonical Functions, e.g., like it is described in this post.

like image 30
Devart Avatar answered Oct 01 '22 22:10

Devart