Vijay P.V
Vijay P.V

Reputation: 145

Date Format in C# Linq

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.

Upvotes: 2

Views: 3131

Answers (3)

kidshaw
kidshaw

Reputation: 3451

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.

Upvotes: 0

Pankaj Gupta
Pankaj Gupta

Reputation: 388

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

Upvotes: 0

Mihai Caracostea
Mihai Caracostea

Reputation: 8466

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.

Upvotes: 1

Related Questions