2

I want to convert date format as 5/Sep/2015 11:53 AM using Linq To Entities. Please see my code below.

public string DeliveryRequiredOn { get; set; }

var _orderdetails =
(
    from o in context.orders
    where o.StoreId == _storeId
        && o.CustomerId == _customerId
        && excludeStatusId.Contains(o.OrderStatusId.Value)
    select new OrderView()
    {
        OrderId = o.OrderId,
        StoreId = o.StoreId,
        DeliveryRequiredOn = o.DeliveryRequiredOn.ToString("dd/MM/yyyy HH:mm:ss"),
        BaseTotal = o.BaseTotal,
    }
).ToList();

How to do this using Linq.

18
  • The date '5/Sep/2015 11:53 AM' does seem to relate to the code. The code has "dd/MM/yyyy HH:mm:ss", but that's a different format. Can you please be more specific as to what you want? Commented Sep 5, 2015 at 6:31
  • I want the format as 5/Sep/2015 11:53 AM. i am trying to do the same. but i am not able to do. please help me Commented Sep 5, 2015 at 6:33
  • That's still not helpful. What does "i am trying to do the same" mean? I don't see where that particular date fits in to your code? Commented Sep 5, 2015 at 6:34
  • 2
    I think you just need MMM as your month specifier. Commented Sep 5, 2015 at 6:39
  • 1
    Also, why is a date being stored as a string in your database? That seems like a bigger problem. Commented Sep 5, 2015 at 6:48

3 Answers 3

1

I belive

"dd/MMM/yyyy hh:mm tt"

would be the format you require.

However, DateTime.ToString(string) is not mapped to a canonical SQL function, according to this. Still, EF might still bring in the database value and compute the formatted value locally, I cannot test this now. You will have to give it a try and see how it goes.

Sign up to request clarification or add additional context in comments.

1 Comment

If it fits not work due to EF no having the ToString function, enumerate the IEnumerable using ToList before doing the Select.
0

Try replacing your format:

DeliveryRequiredOn = o.DeliveryRequiredOn.Value.ToString("dd/MMM/yyyy hh:mm tt");

I am assuming your question means, how to set the string DeliveryRequiredOn to a formatted date string where the month is shown using a short description instead of a number.

6 Comments

when i write the line getting error as No overload for method 'ToString' takes 1 arguments
@VijayP.V As you said o.DeliveryRequiredOn is DateTime so it should not give error on it.
Amended to support nullable date time.
Thanks. but i tried what you have modified code. now when i compile the project i am getting entity error as 'LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object)' method, and this method cannot be translated into a store expression.'
Thanks a lot @kidshaw, its working. I missed AsEnumerable() in linq query. one more thin i want to correct in the date output. now Output is 28/Aug/2015 17:03 PM, i want to display 5:03 instead of 17:03
|
0

Try it

 DateTime dt = new DateTime(2011, 12, 10,11,53,17);
         string str = dt.ToString("dd,MMM,yyyy h :mm :ss tt ");
        Console.WriteLine("Date is " +str);

Hope it will help for you. thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.