开发者

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 AddMin开发者_开发问答utes(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();


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.


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.


You can used: System.Data.Entity.DbFunctions

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜