0

when convert utcnow to local time, it shows error like

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

I am using SQL Server 2014 and not mango db. It will work when I use the function UTCtoLocal outside the query. But I need to use it within the linq query to speed up the execution. There is any other way to do that.

group new
{
    Id = ord.Id,
    ShipmentInfoId = shipment.Id,
    PartnerName = partner.Name,
    PartNumber = ordItem.SellerProductID,
    OrderNumber = ord.UniqueOrderID,
    PartCount = shipment.PartCount,
    DeliveryDate = ord.DeliveryDate,
    IsSentInvoice = shipment.IsSentInvoice != null ? shipment.IsSentInvoice : false,
    IsSentASN = shipment.IsSentASN != null ? shipment.IsSentASN : false,
    ShippingPartCount = 0,
    IsManualEntered = ord.IsManualEntered,
    LastGeneratedInvoiceDate = shipment.LastGeneratedInvoiceDate.Value.UtcToLocal(),
    LastGeneratedASNDate = shipment.LastGeneratedASNDate.Value.UtcToLocal(),
Joehl
  • 3,671
  • 3
  • 25
  • 53
Achu_L
  • 175
  • 1
  • 13
  • Is there a specific reason you couldn't just do a quick search yourself? It would take about 10 seconds, which is less time than it took you to come here, click the Ask Question button, and type the title. – jazb Nov 19 '18 at 07:06
  • Possible duplicate of [Converting DateTime in UTC to my "local" time?](https://stackoverflow.com/questions/35087489/converting-datetime-in-utc-to-my-local-time) – Tetsuya Yamamoto Nov 19 '18 at 07:06
  • I am not using mongo db ,I am using sql 2014 and it should work within lnq query.I need to speed up the query execution. – Achu_L Nov 19 '18 at 07:11

1 Answers1

0

Solution here by playing around with your DateTimeKind.Utc and ToLocalTime()

var dt = new DateTime(2010, 1, 1, 1, 1, 1, DateTimeKind.Utc);
string s = dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss \"GMT\"zzz");
Console.WriteLine(s);

Source: datetime to string with time zone

  • It shows cannot convert from date time to long.I tried using ToLocalTime instead of UtcToLocal().when try within linq query ,it shows same error like to linq to entity does not recognize the method – Achu_L Nov 19 '18 at 07:35