Linq - Select Date from Datetime

Linq - Select Date from DateTime

If it is for presentation purpose, then you can use DataFormatString property. For Example, if you are binding a datasource to a GridView, you could do as;

<asp:BoundField DataField="CREATED_DATE" ...
DataFormatString="{0:d}" ../>

Else you can use EntityFunctions.TruncateTime() which returns the input date without the time portion.

EntityFunctions.TruncateTime(xx.CREATED_DATE)

Your query would be like;

var UserTemplates = (from xx in VDC.SURVEY_TEMPLATE
where xx.USER_ID == userid && xx.IS_ACTIVE == 1
select new
{
xx.TEMPLATE_ID,
xx.TEMPLATE_NAME,
EntityFunctions.TruncateTime(xx.CREATED_DATE) //new like
}).ToList();

Select Date from a Datetime value

It shouild be as simple as just using the Date property in DateTime:

UPDATE: From the 3rd error in the comments I see that ReqDate is A Nullable<DateTime>. Try using the Value property to access the underlying DateTime object:

dbContext.PurchaseOrders.Where(r => r.ReqDate.Value.Date == DateTime.Now.Date)
.ToList();

EDIT

Alternatively, you could try the not-so-pretty option:

dbContext.PurchaseOrders.Where(r => r.ReqDate.ToString("yyyy-MM-dd") == 
DateTime.Now.ToString("yyyy-MM-dd"))
.ToList();

Just convert your DateTime objects to a string formatted to contain the date only.

How to get only Date from datetime column using linq

Call the Date property on any DateTime struct

EffectiveDate.Date;

or call

EffectiveDate.ToShortDateString();

or use the "d" format when calling ToString() more DateTime formats here.

EffectiveDate.ToString("d");

Writing a Linq query could look like this:

someCollection.Select(i => i.EffectiveDate.ToShortDateString());

and if EffectiveDate is nullable you can try:

someCollection
.Where(i => i.EffectiveDate.HasValue)
.Select(i => i.EffectiveDate.Value.ToShortDateString());

DateTime.Date Property

A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).

DateTime.ToShortDateString Method

A string that contains the short date string representation of the current DateTime object.

Linq query and returning DateTime field in a short date format

Solved it by switching to anonymous type. Upvoted helpful answers and comments: starlight54 - removing table name alias form CDOrder, and Lucian Bumb for suggesting a.OrderDate.Month + "/" + a.OrderDate.Day + "/" + a.OrderDate.Year

Below is what worked:

var result = (from a in ctx.Orders
join b in ctx.OrderDetails on a.OrderId Equals b.OrderId
where a.Field1.Equals(id) && b.Field1.Contains("TEST")
select new
{
OrderId = a.OrderId,
Field1 = a.Field1,
Field2=a.Field2,
OrderDate = a.OrderDate.Month + "/" + a.OrderDate.Day + "/" + a.OrderDate.Year
}).AsEnumerable().Select(x => new myClass.CDOrder
{
OrderId = x.OrderId,
Field1 = x.Field1,
Field2=x.Field2,
OrderDate = x.OrderDate
});

How to get Date without Time from DateTime while join tables

First, recognize that there is no built-in type for a date without a time in .NET. At least not yet anyway. So if you want date-only, then you'll need it in a string in some particular format.

You're not going to be able to get the underlying provider of a LINQ query to understand the conversion of DateTime to string. So, you'll need to query for the data in its original form, and then convert it to string after the query results are materialized.

Start with your original query, unmodified from what you showed at the top of your question. Then add the following:

var results = query.AsEnumerable().Select(x=>
new
{
IdObjective = x.IdObjective,
ObjectiveName = x.ObjectiveName,
DateCreation = x.DateCreation.ToShortDateString(),
DateEnd = x.DateEnd.ToShortDateString()
});

You have some options also:

  • If you know you want a List or an Array, then you can use ToList() or ToArray() instead of AsEnumerable().

  • If you want the resulting string to be in a specific format, or use a specific culture, then you can use ToString instead of ToShortDateString

For example, you might want an array containing a standard ISO-8601 date string and you might want to use the Invariant culture to avoid side effect when the current culture uses a non-Gregorian calendar system.

var results = query.ToArray().Select(x=>
new
{
IdObjective = x.IdObjective,
ObjectiveName = x.ObjectiveName,
DateCreation = x.DateCreation.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
DateEnd = x.DateEnd.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
});


Related Topics



Leave a reply



Submit