I'm trying to get results if there are records where the date falls between two fields: start and finish. The linq query I have doesn't seem to return any results:
var alerts = from q in db.AlertMsgs
where (q.Start >= DateTime.Now
&& q.Finish <= DateTime.Now)
select q;
Any suggestions on why this is? I've also tried
var alerts = from q in AlertMsgs.Where(q => q.Start.CompareTo(DateTime.Now) > 0
&& q.Finish.CompareTo(DateTime.Now) < 1)
select q;
without any luck either.
You have your conditions backwards. Try:
var alerts = from q in db.AlertMsgs
where (q.Start < DateTime.Now && q.Finish > DateTime.Now)
select q;
Or, using the method syntax (and <=/>=, in case you want that comparison):
var alerts = db.AlertMsgs
.Where(q => q.Start <= DateTime.Now && q.Finish >= DateTime.Now);
This will give you all messages which have already started (Start < Now) and have not yet finished (Finish > Now). What you were searching for was messages which haven't started yet (or started exactly now), but which have already finished (or finished exactly now). This would only return an item which had the same start and end time, and where both times were exactly DateTime.Now.
Related
In the first the business is car rental system .
I want to get all car where has no orders in interval selected by user
public List<Car> SearchCar(DateTime pickdate, DateTime dropdate)
{
var db = new CarRentalDBEntities();
var temp = new List<Car>();
temp = db.Cars.Where(item =>
!item.Orders.Any
(e => e.PickUpDateTime >= pickdate && dropdate <= e.DropDataTime)
).ToList();
return temp;
}
this is the last code I write
the error is : cars still comes if order intersect with interval user choosed
As per my comment, you probably want to be checking if either the pickup date is in the range or the return date is in the range, or if the rental period is longer than the entire range:
e => (pickdate <= e.PickUpDateTime && e.PickUpDateTime < dropdate) || //picked up in period
(pickdate <= e.DropDataTime && e.DropDataTime < dropdate) || //dropped off in period
(e.PickUpDateTime < pickdate && e.DropDataTime > dropdate) //rental spans period
Notes: typo in your DropDataTime
Maybe you can use:
var temp = db.Cars.Where(item =>
item.Orders.All(e =>
e.DropDataTime <= pickdate
||
e.PickUpDateTime >= dropdate
)
).ToList();
For all existing orders e, either they finish their order in time, or else they only need the car after.
This assumes that all existing orders e on the car are "sane" in the sense that their pick-up is earlier than their drop time.
If i understand what you are trying to do,
I made the following modifications
I used All instead of !Any i find it easier to read
I put the Db Context In a using statement, good practice
I returned the output directly (no need for temp variable)
The premise as i understand it is to return all cars not and order
public List<Car> SearchCar(DateTime pickdate, DateTime dropdate)
{
using (var db = new CarRentalDBEntities())
{
return db.Cars.Where(item => item.Orders.All(e =>
// where order pickup date is less Pick or greater than drop
(e.PickUpDateTime < pickdate || e.PickUpDateTime > dropdate) &&
// where order Drop date is less Pick or greater than drop
(e.DropDataTime < pickdate || e.DropDataTime > dropdate)))
.ToList();
}
}
var MyCours = Db.COURS.Where(C => C.CLASSE_ID == ClassID
&& DateTime.Now>= C.START_DATE
&& DateTime.Now <= C.END_DATE)
.ToList();
Some change still dont work !
A likely problem is that the provider can't project DateTime.Compare into a SQL statement. There is potentially also a logical error in the direction of comparison (unless you really want enddate < now < startdate), and I would also suggest using .ToList() to materialize into a list:
var theTimeNow = DateTime.Now;
var MyCours = Db.COURS.Where(C => C.CLASSE_ID == ClassID
&& theTimeNow >= C.START_DATE
&& theTimeNow <= C.END_DATE)
.ToList();
Projecting DateTime.Now into a variable isolates the non-determinism of it, i.e. to ensure that both comparisons are against the same time.
I want my query to stop displaying time and just the date. This is what I've tried to far:
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo
&& z.ActivityDate >= EndDateTo
&& z.Indepth == false
select new
{
Date = new DateTime(z.ActivityDate.Year, z.ActivityDate.Month, z.ActivityDate.Day),
Subject = z.Subject
}).ToList();
And
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo
&& z.ActivityDate >= EndDateTo
&& z.Indepth == false
select new
{
Date = z.ActivityDate.Date,
Subject = z.Subject
}).ToList();
And both didn't work.
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression. when trying to apply a string method.
You can use anyDate.ToString("ddMMyyyy");//any preferred format.
Not sure if that is what you are looking for!
Your queries return objects with Date & Subject properties.
In the Date property you are passing a DateTime object. In order to display the short date you have a "ToShortDateString()" function on a date.
If you dont want to work with a date and prefer selecting a string, then do the conversion inside the linq query.
Use this if you want to return strings:
var q = (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo && z.ActivityDate >= EndDateTo && z.Indepth == false
select new { Date = z.ActivityDate.Date.ToShortDateString(), Subject = z.Subject }).ToList();
You would need to perform the formatting at the time of the binding. As you don't show the actual binding code, it is hard to specifically address your situation but lets look at what happens in your query:
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo && z.ActivityDate >= EndDateTo && z.Indepth == false
select new { Date = z.ActivityDate.Date, Subject = z.Subject }).ToList();
Once LINQ handles this query, the resulting Query variable should be of type List<DateTime>. The way you have the query working you would return a list of DateTimes in a format like this:
2014-04-23 00:00:00
2014-03-28 00:00:00
etc...
In order to bind this without the time value, you need to call ToString() on each element (or the desired element) of the list at the time of binding.
Assuming you are using a ListBox or something similar you could write the following:
foreach (var date in myList) //this is the resultant list from the query
{
listBox1.Items.Add(date.ToString("MM/dd/yyyy");
}
If you are literally binding to a DataSource property, you will need to convert your List<DateTime> to a List<string> with the formatted values.
ToShortDateString() may help you.
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo
&& z.ActivityDate >= EndDateTo
&& z.Indepth == false
select new
{
Date = z.ActivityDate.ToShortDateString(),
Subject = z.Subject
}).ToList();
convert date into string like below
string stringDate=string.empty;
stringDate=Convert.ToDateTime("2014-04-23 00:00:00").ToShortDateString();
it will give output like
2014-04-23
I am trying to make a query on LINQ using a GroupBy, where on the Select statement I try to make some kind of subquery. The idea is that I have 7 days to look for, I need to get the average value of these last 7 days but also the current value for the current day. So I have tried the following code:
var oDate = DateTime.UtcNow;
var startDate = oDate.AddWorkingDays(-7);
var endDate = oDate.AddWorkingDays(-1);
var races = RaceTimes.Where(
x =>
x.ODate >= startDate && x.ODate <= oDate && x.Status == "Finished")
.GroupBy(x => x.Athletes.Name).Select(x => new
{
Name = x.Key,
Description = "Just a Random Description for now",
ODate = oDate,
SevenDaysAvg = (int?)x.Where(y => y.ODate >= startDate && y.ODate <= endDate).Average(y => SqlFunctions.DateDiff("s", y.StartTime, y.EndTime)),
RaceTime = (int?)x.Where(y => y.ODate == oDate).Average(y => SqlFunctions.DateDiff("s", y.StartTime, y.EndTime))
}).ToList().Where(x => x.RaceTime!= null).ToList();
The SevenDaysAvg (average) works fine, it returns me the average of the last 7 days races. However, Latency is always returning me null.
I am trying to make the translation of my query from SQL, which works fine:
...
AVG(case when h.ODate BETWEEN '20140219' AND '20140227' then DATEDIFF(SECOND, h.StartTime, h.EndTime) else null end) AS SevenDaysAvg,
AVG(case when h.ODate = '20140227' then DATEDIFF(SECOND, h.StartTime, h.EndTime) else null end) AS RaceTime,
...
So what am I doing wrong in my LINQ query? This what I am doing is not possible? I've also tried another aproach. First calculate all averages, and then in the main query have something like:
SevenDaysAvg = averages.Where(y => y.Name== x.Athletes.Name).Select(y => y.SevenDaysAvg).FirstOrDefault()
Once again, this approach didnt work, it keeps giving me empty results. Whats wrong here?
I'm trying to make a Linq to SQL query that returns Date grouping results. The challenge is about grouping daily/weekly/monthly/quarterly depends of a enumerable parameter (periodicity). Below, my current Linq to SQL query:
var TotalGroupedInvoices = from c in entidades.InvoicesView
group c by (periodo == periodicity.Daily) ? c.InvoiceDate.Date :
period == periodicity.Weekly? c.InvoiceDate.Date.AddDays(-(double)c.InvoiceDate.DayOfWeek) :
period == periodicity.Monthly? new DateTime(c.InvoiceDate.Year,c.InvoiceDate.Month ,1) :
period == periodicity.Quarterly? new DateTime(c.InvoiceDate.Year, c.InvoiceDate.Month - (c.InvoiceDate.Month % 3) +1, 1) :
period == periodicity.Anual ? new DateTime(c.InvoiceDate.Year, 1, 1) : inicio into Periods
select new
{
period = Periods.Key,
Total = Periodos.Sum(c => c.Total)
};
For clarification, take a look at the quarterly period code fragment:
period == periodicity.Quarterly? new DateTime(c.InvoiceDate.Year, c.InvoiceDate.Month - (c.InvoiceDate.Month % 3) +1, 1)
Thus, for dates into first quarter like: January/12/2012, January/15/2012, or March/20/2012 i get all of them grouped at the quarter begginig: January/1/2012, so it's works as expected.
First I wonder about the query efficiency. What do you think about this? Maybe it would be better to translate periods in integers for SQL Server efficiency and re-translate to date periods on client, but i'm not sure about this.
On the other hand, the weekly group works grouping dates weekly into the first sunday of each week:
period == periodicity.Weekly? c.InvoiceDate.Date.AddDays(-(double)c.InvoiceDate.DayOfWeek)
...but that's incorrect for me because i'm from Spain and weeks start on Monday. How can i fix the week groups to take this into account?
So, summarizing:
What about this Linq to SQL query efficiency?
How can i group weekly by this but considering weeks from Monday to Sunday?
Thanks a lot!
PD: sorry for my English level.
To answer the first part, you don't really want to transmit the enum value into the databaes and let it make the decision of which code branch to use. You have all the information to make the decision locally.
Expression<Func<InvoicesView, DateTime>> groupingExpr = c => c.InvoiceDate.Date;
if (period == periodicity.Weekly)
{
groupingExpr = c => c.InvoiceDate.Date.AddDays(-(double)c.InvoiceDate.DayOfWeek);
}
else if (period == periodicity.Monthly)
{
groupingExpr = c => new DateTime(c.InvoiceDate.Year,c.InvoiceDate.Month ,1);
}
else if (period == periodicity.Quarterly)
{
groupingExpr = c => new DateTime(c.InvoiceDate.Year, c.InvoiceDate.Month - (c.InvoiceDate.Month % 3) +1, 1);
}
else if (period == periodicity.Anual
{
groupingExpr = c => new DateTime(c.InvoiceDate.Year, 1, 1);
}
var TotalGroupedInvoices = entidades.InvoicesView
.GroupBy(groupingExpr)
.Select(grouped => new {
period = grouped.Key,
Total = grouped.Sum(c => c.Total)
});
Here's the best I can do to blend the groupingExpr with query comprehension syntax:
var TotalGroupedInvoices = from grouped in entidades.InvoicesView.GroupBy(groupingExpr)
select new {
period = grouped.Key,
Total = grouped.Sum(c => c.Total)
};
To answer the second part, you just need to do some "day" arithmetic. You can do this inline or as a separate function (eg extension method):
public static DateTime FirstDayOfWeek (this DateTime date)
{
double offset = (date.DayOfWeek == DayOfWeek.Sunday) ? -6 : (DayOfWeek.Monday - date.DayOfWeek);
return date.AddDays(offset);
}
Then in your LINQ:
period == periodicity.Weekly ? c.InvoiceDate.FirstDayOfWeek
However, given that you are retrieving this from SQL you may just want to look at the date functions there eg DATEPART and return them in your queries