Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert datetime to a formatted string inside a LINQ-to-entities query

How can I convert DateTime into a formatted string?

This is the line in the following query that needs help:

StartDate = string.Format("{0:dd.MM.yy}", p.StartDate) 

The whole query:

var offer = (from p in dc.CustomerOffer              join q in dc.OffersInBranch              on p.ID equals q.OfferID              where q.BranchID == singleLoc.LocationID              let value = (p.OriginalPrice - p.NewPrice) * 100 / p.OriginalPrice              orderby value descending              select new Offer()              {                  Title = p.OfferTitle,                  Description = p.Description,                  BestOffer = value,                  ID = p.ID,                  LocationID = q.BranchID,                  LocationName = q.CustomerBranch.BranchName,                  OriginalPrice = SqlFunctions.StringConvert((decimal)p.OriginalPrice),                  NewPrice = SqlFunctions.StringConvert((decimal)p.NewPrice),                  StartDate = string.Format("{0:dd.MM.yy}", p.StartDate)              }).First(); 

I get the following error message:

LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

like image 913
Crime Master Gogo Avatar asked Nov 03 '11 18:11

Crime Master Gogo


People also ask

How to convert DateTime to string in LINQ query?

DateTime to string conversion using LINQ(from .... select new { /* stuff */, Date = c. Date }) . AsEnumerable() . Select(p => new Post { /* stuff */, PostingDate = p.

How do I change the date format in Entity Framework?

Entity Framework DateTime format when editing entry Id... and ... Date.... It's defined as so:...public class Timetable { public int Id { get; set; } [Required, Column(TypeName = "Date"), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")] public DateTime Date { get; set; } } ... I then scaffolded some basic...

What is include in LINQ query C#?

Introduction to LINQ Include. LINQ include helps out to include the related entities which loaded from the database. It allows retrieving the similar entities to be read from database in a same query. LINQ Include() which point towards similar entities must read from the database to get in a single query.

What is EF LINQ?

LINQ allows you to use C# (or your . NET language of choice) to write strongly typed queries. It uses your derived context and entity classes to reference database objects. EF Core passes a representation of the LINQ query to the database provider.


1 Answers

Another option is using SqlFunctions.DateName, your code will be like this:

var offer = (from p in dc.CustomerOffer                  join q in dc.OffersInBranch                      on p.ID equals q.OfferID                  where q.BranchID == singleLoc.LocationID                  let value = (p.OriginalPrice - p.NewPrice) * 100 / p.OriginalPrice                  orderby value descending                  select new                  {                      Title = p.OfferTitle,                      Description = p.Description,                      BestOffer = value,                      ID = p.ID,                      LocationID = q.BranchID,                      LocationName = q.CustomerBranch.BranchName,                      OriginalPrice = SqlFunctions.StringConvert((decimal)p.OriginalPrice),                      NewPrice = SqlFunctions.StringConvert((decimal)p.NewPrice),                      StartDate = SqlFunctions.DateName("day", p.StartDate) + "/" + SqlFunctions.DateName("month", p.StartDate) + "/" +  SqlFunctions.DateName("year", p.StartDate)                  }) 

I found it useful if you don't want to add an extra select new block.

like image 149
Alberto Montellano Avatar answered Sep 19 '22 15:09

Alberto Montellano