Select Date from a Datetime value - linq-to-sql

I want to select Date part from a DateTime value using Linq to Sql or sql lambda.
Here is my scenario.
dbContext.PurchaseOrders.Where(r => r.ReqDate == DateTime.Now).ToList();
Here ReqDate is a DateTime field. I want to select all the purchase orders for today.

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.

I have an answer that will return a list. Long way I know but I could not find a solution anywhere.
I hope this save someone else the frustration. I have been searching and searching to find an answer to this issue.
It does a group by on datetime to return a list of dates with no time. In my case it was to load a combo box of dates to choose from.
var mADates = from ta in db.AM_Assignments
orderby ta.Assignment_DTM
group ta by ta.Assignment_DTM.Date;
List<DateTime> dtx = new List<DateTime>();
foreach (var dt in mADates)
{
dtx.Add(Convert.ToDateTime(dt.Key.ToShortDateString()));
}
return dtx;

Use the Subtract method.
dbContext.PurchaseOrders.Where(r => DateTime.Now.Subtract(r.ReqDate).Days == 0).ToList();

try this:
dbContext.PurchaseOrders.Where(r => r.ReqDate.Date == DateTime.Today).ToList();

.Date creates a 00:00:00 time so time is not compared
dbContext.PurchaseOrders.Where(r => r.ReqDate.Date == DateTime.Now.Date).ToList();

There are some LINQ2SQL functions that imitate native SQL ones. One of them - truncation of time from DateTime? or DateTimeOffset?.
var today = DateTime.Now.Date;
dbContext.PurchaseOrders.Where(r => DbFunctions.TruncateTime(r.ReqDate).Value == today).ToList();

More compact version of #Angus 's code
var mADates = from ta in db.AM_Assignments
orderby ta.Assignment_DTM
group ta by ta.Assignment_DTM.Date;
return mADates.Select(dt => Convert.ToDateTime(dt.Key.ToShortDateString())).ToList();

Related

extract value from a json object in a database column using linq

Suppose I've two columns in my database table Employees as below :
EmpId ImageData
----- --------------------------------------------
1 {"path":"~/Content/Image.png","type":".pdf"}
Now, How to place a condition in my linq where() clause to check if the path of ImageData is equals to ~/Content/Image.png?
I want to do something like this :
var data = db.Employees
.Where(x => JObject.Parse(x.ImageData)["path"] == "~/Content/Image.png")
.ToList();
Is it possible? Or, What is the right way to achieve this? Thanks :)
Update
As per Steve Ford's suggestion, I'm having the below exception :
LINQ to Entities does not recognize the method 'Newtonsoft.Json.Linq.JToken get_Item(System.String)' method, and this method cannot be translated into a store expression.
Try converting the JToken you receive from JObject.Parse to a string:
var data = db.Employees
.Where(x => (string)JObject.Parse(x.ImageData)["path"] == "~/Content/Image.png")
.ToList();

ZF2 Query WHERE clause with DATE() for datetime column

I have a column with datetime data type and I want to build a SQL query in Zend Framework2 which compare date part with user input date.
Need to build similar part as DATE(datetime column) = '2014-09-16' with;
$select->where();
would be very grateful if someone could help on this.
Use like this:
$date = '2014.05.24';
$select->where('date(expecting_date) = "'.$date.'"');
You should use predicate expression for these kind of conditions, like :
$select = new \Zend\Db\Sql\Select(table name);
$select->where(new \Zend\Db\Sql\Predicate\Expression('DATE(datetime) = ?', '2014-09-16'));

Use of custom expression in LINQ leads to a query for each use

I have the following problem: In our database we record helpdesk tickets and we book hours under tickets. Between those is a visit report. So it is: ticket => visitreport => hours.
Hours have a certain 'kind' which is not determined by a type indicator in the hour record, but compiled by checking various properties of an hour. For example, an hour which has a customer but is not a service hour is always an invoice hour.
Last thing I want is that the definitions of those 'kinds' roam everywhere in the code. They must be at one place. Second, I want to be able to calculate totals of hours from various collections of hours. For example, a flattened collection of tickets with a certain date and a certain customer. Or all registrations which are marked as 'solution'.
I have decided to use a 'layered' database access approach. The same functions may provide data for screen representation but also for a report in .pdf . So the first step gathers all relevant data. That can be used for .pdf creation, but also for screen representation. In that case, it must be paged and ordered in a second step. That way I don't need separate queries which basically use the same data.
The amount of data may be large, like the creation of year totals. So the data from the first step should be queryable, not enumerable. To ensure I stay queryable even when I add the summation of hours in the results, I made the following function:
public static decimal TreeHours(this IEnumerable<Uren> h, FactHourType ht)
{
IQueryable<Uren> hours = h.AsQueryable();
ParameterExpression pe = Expression.Parameter(typeof(Uren), "Uren");
Expression left = Expression.Property(pe, typeof(Uren).GetProperty("IsOsab"));
Expression right = Expression.Constant(true, typeof(Boolean));
Expression isOsab = Expression.Equal(Expression.Convert(left, typeof(Boolean)), Expression.Convert(right, typeof(Boolean)));
left = Expression.Property(pe, typeof(Uren).GetProperty("IsKlant"));
right = Expression.Constant(true, typeof(Boolean));
Expression isCustomer = Expression.Equal(Expression.Convert(left, typeof(Boolean)), Expression.Convert(right, typeof(Boolean)));
Expression notOsab;
Expression notCustomer;
Expression final;
switch (ht)
{
case FactHourType.Invoice:
notOsab = Expression.Not(isOsab);
final = Expression.And(notOsab, isCustomer);
break;
case FactHourType.NotInvoice:
notOsab = Expression.Not(isOsab);
notCustomer = Expression.Not(isCustomer);
final = Expression.And(notOsab, notCustomer);
break;
case FactHourType.OSAB:
final = Expression.And(isOsab, isCustomer);
break;
case FactHourType.OsabInvoice:
final = Expression.Equal(isCustomer, Expression.Constant(true, typeof(Boolean)));
break;
case FactHourType.Total:
final = Expression.Constant(true, typeof(Boolean));
break;
default:
throw new Exception("");
}
MethodCallExpression whereCallExpression = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { hours.ElementType },
hours.Expression,
Expression.Lambda<Func<Uren, bool>>(final, new ParameterExpression[] { pe })
);
IQueryable<Uren> result = hours.Provider.CreateQuery<Uren>(whereCallExpression);
return result.Sum(u => u.Uren1);
}
The idea behind this function is that it should remain queryable so that I don't switch a shipload of data to enumerable.
I managed to stay queryable until the end. In step 1 I gather the raw data. In step 2 I order the data and subsequently I page it. In step 3 the data is converted to JSon and sent to the client. It totals hours by ticket.
The problem is: I get one query for the hours for each ticket. That's hundreds of queries! That's too much...
I tried the following approach:
DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Ticket>(t => t.Bezoekrapport);
options.LoadWith<Bezoekrapport>(b => b.Urens);
dc.LoadOptions = options;
Bezoekrapport is simply Dutch for 'visitreport'. When I look at the query which retrieves the tickets, I see it joins the Bezoekrapport/visitreport but not the hours which are attached to it.
A second approach I have used is manually joining the hours in LINQ, but that does not work as well.
I must do something wrong. What is the best approach here?
The following code snippets are how I retrieve the data. Upon calling toList() on strHours in the last step, I get a hailstorm of queries. I've been trying for two days to work around it but it just doesn't work... Something must be wrong in my approach or in the function TreeHours.
Step 1:
IQueryable<RelationHoursTicketItem> HoursByTicket =
from Ticket t in allTickets
let RemarkSolved = t.TicketOpmerkings.SingleOrDefault(tr => tr.IsOplossing)
let hours = t.Bezoekrapport.Urens.
Where(h =>
(dateFrom == null || h.Datum >= dateFrom)
&& (dateTo == null || h.Datum <= dateTo)
&& h.Uren1 > 0)
select new RelationHoursTicketItem
{
Date = t.DatumCreatie,
DateSolved = RemarkSolved == null ? (DateTime?)null : RemarkSolved.Datum,
Ticket = t,
Relatie = t.Relatie,
HoursOsab = hours.TreeHours(FactHourType.OSAB),
HoursInvoice = hours.TreeHours(FactHourType.Invoice),
HoursNonInvoice = hours.TreeHours(FactHourType.NotInvoice),
HoursOsabInvoice = hours.TreeHours(FactHourType.OsabInvoice),
TicketNr = t.Id,
TicketName = t.Titel,
TicketCategorie = t.TicketCategorie,
TicketPriority = t.TicketPrioriteit,
TicketRemark = RemarkSolved
};
Step 2
sort = sort ?? "TicketNr";
IQueryable<RelationHoursTicketItem> hoursByTicket = GetRelationHours(relation, dateFrom, dateTo, withBranches);
IOrderedQueryable<RelationHoursTicketItem> orderedResults;
if (dir == "ASC")
{
orderedResults = hoursByTicket.OrderBy(sort);
}
else
{
orderedResults = hoursByTicket.OrderByDescending(sort);
}
IEnumerable<RelationHoursTicketItem> pagedResults = orderedResults.Skip(start ?? 0).Take(limit ?? 25);
records = hoursByTicket.Count();
return pagedResults;
Step 3:
IEnumerable<RelationHoursTicketItem> hours = _hourReportService.GetRelationReportHours(relation, dateFrom, dateTo, metFilialen, start, limit, dir, sort, out records);
var strHours = hours.Select(h => new
{
h.TicketNr,
h.TicketName,
RelationName = h.Relatie.Naam,
h.Date,
TicketPriority = h.TicketPriority.Naam,
h.DateSolved,
TicketCategorie = h.TicketCategorie == null ? "" : h.TicketCategorie.Naam,
TicketRemark = h.TicketRemark == null ? "" : h.TicketRemark.Opmerking,
h.HoursOsab,
h.HoursInvoice,
h.HoursNonInvoice,
h.HoursOsabInvoice
});
I don't think your TreeHours extension method can be converted to SQL by LINQ in one go. So are evaluated on execution of each constructor of the row, causing a 4 calls to the database in this case per row.
I would simplfy your LINQ query to return you the raw data from SQL, using a simple JOIN to get all tickets and there hours. I would then group and filter the Hours by type in memory. Otherwise, if you really need to perform your operations in SQL then look at the CompiledQuery.Compile method. This should be able to handle not making a query per row. I'm not sure you'd get the switch in there but you may be able to convert it using the ?: operator.

Order By missing in linqtosql select random row

I have the following linq query :
IQueryable<Message> messagesQuery = (from message in _context.Db.Messages
where message.MessageListId == item.MessageListId
&&
!_context.Db.ScheduleXMessages.Any(x => x.MessageId == message.MessageId && x.ScheduleId == item.ScheduleId)
select message);
if (randomSendMessage)
return (from mq in messagesQuery orderby Guid.NewGuid() select mq).FirstOrDefault();
return (from mq in messagesQuery orderby mq.OrderIndex select mq).FirstOrDefault();
Now, if randomSendMessage is true, the order by doesn't get added to the select. if it's false, then the Order By OrderIndex is added to the select.
Any ideas on what might be going on ?
Thanks.
Edit Nope, it doesn't work if I select the column upfront. It just sends the guid as a parameter and instead of doing an orderby newid() , it does it by using one single guid
If you want to get random results with LINQ to SQL you can use Marc Gravell's solution here. I recently explained how to setup a partial class to use it in this post. Otherwise you may have to setup a stored procedure or UDF.
EDIT: I took a look at the blog post you mentioned. The author actually pointed to Marc's solution as an alternative :)
Not exactly sure what is going on, but my guess would be it doesn't support that type of ordering... possibly because it's not defined within the select list. Try the alternative:
IQueryable<Message> messagesQuery = (from message in _context.Db.Messages
where message.MessageListId == item.MessageListId
&&
!_context.Db.ScheduleXMessages.Any(x => x.MessageId == message.MessageId && x.ScheduleId == item.ScheduleId)
select new { Message = message, ID = Guid.NewGuid() });
if (randomSendMessage)
return (from mq in messagesQuery orderby mq.ID select mq.Message).FirstOrDefault();
return (from mq in messagesQuery orderby mq.Message.OrderIndex select mq.Message).FirstOrDefault();
to see if that resolved it by storing the ID within the record.
I guess it should have bee bloody obvious that L2S wouldn't know what to do with Guid.NewGuid() :).
I found an answer here : http://weblogs.asp.net/fmarguerie/archive/2008/01/10/randomizing-linq-to-sql-queries.aspx
It works.

How do you compare dates in a LINQ query?

I am tring to compare a date from a asp calendar control to a date in the table.... here's what i have... it doesn't like the == ?
var query = from details in db.TD_TravelCalendar_Details
where details.StartDate == calStartDate.SelectedDate
&& details.EndDate == calEndDate.SelectedDate
select details;
In order for your query to work both details.StartDate and calStartDate.SelectedDate must be typed as System.DateTime.
What error are you receiving? Most likely one of these properties is a string and will need to be parsed into a DateTime instance for comparison.
What is the Type of details.StartDate and details.EndDate? Is it String? Maybe that's the problem. If the database type is String you should Parse the Date String into a DateTime and compare it with the calendar's selected date then.