How can I do this on linq?
CONVERT(date,DATEADD(hour,9,TestDate)) = CONVERT(date, GETUTCDATE())
on UTC date.
I did this without success:
a.Test.TestDate.AddHours(9) == DateTime.UtcNow
For EntityFramework you would need to use a combination of:
SqlFunctions.GetUtcDate()
DbFunctions.AddHours()
DbFunctions.TruncateTime()
Example:
DbFunctions.TruncateTime(DbFunctions.AddHours(a.Test.TestDate, 9)) ==
DbFunctions.TruncateTime(SqlFunctions.GetUtcDate())
Related
Is it possible to construct a query in which I can retrieve week day name from a date into a separate column or variable?
I know I can very easily do this on .NET side but would like it to be in the query.
You can also use SqlFunctions...
var results=context.Listings
.Select(l=>System.Data.Entity.SqlServer.SqlFunctions.DateName("dw",l.modify_date));
Of course, this only works when using a SQL Server. Methods that are cross-database, would be to use EntityFunctions.DateDiff with a known date to get the number of days between whatever and a known prior sunday, then modulus 7, then convert to a string.
It is possible you will need to build your query which returns day names and then join to your result on day number
int[] dayNum={1,2,3,4,5,6,7};
var result = from d in dayNum
let dayOfWeek= (d == 1 ? "Monday" :
d==2 ? "Tuesday" :
d==3 ? "Wednesday" :
d==4 ? "Thursday" :
d==5 ? "Friday" :
d==6 ? "Saturday" :
d==7 ? "Sunday":"")
let dn = d
group d by new {dayOfWeek, dn} into dw
select new { dw.Key.dayOfWeek, dw.Key.dn};
The result of this will be
I've to calculate the différence between two Dates : TODAY() and DATE_DEB_VAC.
With Oracle, it's kinda easy : TODAY()-DATE_DEB_VAC -> give the number of day between those 2 date.
But I've to do it with in an ETL (GENIO). I've a column to stock it like that :
NUMBER_DAY_DIFF (NUMBER 10) = TODAY()-DATE_DEB_VAC. But it's impossible to stock it cause it's 2 date.
How can i do this ? :(
You can try the val function of GENIO ETL
VAL(TODAY()-DATE_DEB_VAC)
this is equivalent to to_numbre in Oracle
NUMBER_DAY_DIFF (NUMBER 10) = DATEDIFF (TODAY; DATE_DEB_VAC)
Should give you what you need.
I have the following code:
DateTime timeStamp = Convert.ToDateTime(Request.QueryString["TimeStamp"]);
var result = (from rs in db.VRec
where
rs.TimeStamp == timeStamp &&
rs.Fixure == wFixture
select rs).ToList();
The result shows 0 even though the correct timeStamp is passed.
If I remove the part where I do the TimeStamp comparison:
rs.TimeStamp == timeStamp
The code works fine.
Any idea on why the datetime comparison may not be working?
DateTime has a pretty fine resolution - likely you are comparing timestamps that only differ in milliseconds, which will fail. You probably want something like:
DateTime now = DateTime.Now;
DateTime then = now.Add(TimeSpan.FromMilliseconds(1));
const int EPSILON_MS = 10;
if(now.Subtract(then).TotalMilliseconds < EPSILON_MS)
{
Console.WriteLine("More or less equal!");
}
Linq converts DateTime arguments to DateTime2 in the sql query executed.
That is, when you do the comparison the actual sql executed will compare a DateTime to a DateTime2. This comparison will "cast" the DateTime to a DateTime2 and the millisecond part will be expanded to a greater resolution (in an odd way in my opinion, please enlighten me).
Try to execute the following sql:
declare #d1 datetime = '2016-08-24 06:53:01.383'
declare #d2 datetime2 = '2016-08-24 06:53:01.383'
declare #d3 datetime2 = #d1
select #d1 as 'd1', #d2 'd2', #d3 'converted'
select (case when (#d1 = #d2) then 'True' else 'False' end) as 'Equal',
(case when (#d1 > #d2) then 'True' else 'False' end) as 'd1 greatest'
From the question, I do not know if you want to compare the date with time or only the date part. If you only want to compare date then following would work
var result = (from rs in db.VRec
where
rs.TimeStamp.Date == timeStamp.Date &&
rs.Fixure == wFixture
select rs).ToList();
Since you are using some reference to db, it gives me a feeling that you are fetching your records from database (which ORM you are using is not obvious from the question or tags). Assuming that you are using Entity framework the above query will fail with exception that .Date has no direct translation to sql. If so you can rewrite the query as following to make it work.
var result = (from rs in db.VRec
where
rs.TimeStamp.Day == timeStamp.Day &&
rs.TimeStamp.Month == timeStamp.Month &&
rs.TimeStamp.Year == timeStamp.Year &&
rs.Fixure == wFixture
select rs).ToList();
The benefit of this approach is that you can compare properties to arbitrary deep level i.e you can compare Hours, Minutes,Seconds etc. in your query. The second query is tested in Entity framework 5.
In linq how can i diffentiate the date, like in sql we use
datediff(mm,getdate(),getdate())=0
now how can i write in linq, can any body help me thank you.
I think this is what you want:
var query = from entity in entities
where entity.SomeDateTime.Date == anotherDateTime.Date
select entity;
This only compares dates and not times.
You can use this code for comparing two dates using linq in C#
DateTime d1 = myDate1;
DateTime d2 = myDate2;
TimeSpan t1 = d2.Subtract(d1);
totalDays = t1.Days;
There's nothing special in Linq about date and time handling, so we should look at the plain .Net facilities.
A DateTime in C# is a date and time.
To get just the date component in C#, try this:
var now = DateTime.UtcNow; // or DateTime.Now for local time/day
DateTime today = now.Date;
This will give you the date and time at midnight of the same day.
If you want to get the difference in days between two DateTime objects:
public TimeSpan DiffDates(DateTime d1, DateTime d2)
{
return d1.Date - d2.Date;
}
// ...
if(DiffDates(dateTime1, dateTime2) == TimeSpan.Zero)
{
// ...
}
Alternately you could check directly if the dates are equal:
if(dateTime1.Date == dateTime2.Date)
{
// ...
}
If you skip the .Date property, then you will get incorrect results.
I trying to compare two dates (DateTime) in nHibernate linq:
query = query.Where(l => (l.datCriacao.Date == dtLote.Date)
but I am getting the error:
NHibernate.QueryException: could not resolve property: datCriacao.Date of: SAGP.Entities.Lote
Anyone knows how I can solve this? Thanks
I solved the problem doing a between with the dates:
DateTime initialDate, finalDate;
initialDate= DateEntity.Date;
finalDate= new DateTime(DateEntity.Year, DateEntity.Month, DateEntity.Day, 23, 59, 59);
query = query.Where(l => (((l.dateEntity>= initialDate) && (l.dateEntity<= finalDate))
This is super old, but I'd add to jaspion's example as:
query = query.Where(l => (l.datCriacao >= dtLote.Date && l.datCriacao < dtLote.Date.AddDays(1))
You can check the condition like this
var nextDay = DateTime.Today.AddDays(1);
query = query.Where(l => (l.datCriacao >= dtLote && l.datCriacao < nextDay);
here you'll get the records on dtLote date as we checking between dtLote and dtLote+1 day (00:00:00) we'll get today's date record only what ever may be the time...