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.
DateTime to string conversion using LINQ(from .... select new { /* stuff */, Date = c. Date }) . AsEnumerable() . Select(p => new Post { /* stuff */, PostingDate = p.
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...
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With