BETWEEN CAST(GETDATE() AS DATE) AND DATEADD(WEEK, 4, CAST(GETDATE() AS DATE))
This is how you do it in mssql. How can I do it using dynamic linq (or whatever it's called - like not C#, but strings).
I'll appreciate any help.
I assume this is C#.
if so, you are probably looking for something like this:
var theDate = {some date};
if (theDate <= DateTime.Today && theDate >= DateTime.Today.AddDays(-4 * 7))
{
// stuff goes here
}
In terms of LINQ, it really depends on what you are trying to do, but this should give you the general idea:
var foos = from foo in SomeQueryable
where foo.Date <= DateTime.Today
&& foo.Date >= DateTime.Today.AddDays(-4 * 7)
select foo;
var qryResult = (from tbl in dbcontext.Yourtable
where tbl.CheckDate >= DateTime.Today
&& tbl.CheckDate <= System.Globalization.CultureInfo.InvariantCulture.Calendar.AddWeeks(DateTime.Today, 4) )
select tbl
).ToList();
If you just need to know how to compare date in C# then:
Create an extension:
public static DateTime AddWeeks(this DateTime date, int weeks)
{
return date.AddDays(7*weeks);
}
And so in Linq:
var now = DateTime.Now;
whatever.Where(d => now.AddWeeks(-4) < d && d < now)
Related
i have 2 int column in sql database one for month and one for year
i want to query this against Database like this
DateFrom is dateTime and also DateTo is DataTime
var result = GetAll().Where(x =>
(x.InvoiceMonth >= DateFrom.Month && x.InvoiceYear >= DateFrom.Year)
&&
(x.InvoiceMonth <= DateTo.Month && x.InvoiceYear <= DateTo.Year));
but the problem here if user sent 1/1/2021 to 1/1/2023
it will just return Jan 2021 and Jan 2022and Jan 2023
the correct return should return all things between 2 dates
You can construct the date with the data you have
var result = GetAll().Where(x =>
(new DateTime(x.InvoiceYear, x.InvoiceMonth, 1) >=
new DateTime(DateFrom.InvoiceYear, DateFrom.InvoiceMonth, 1))
&&
(new DateTime(x.InvoiceYear, x.InvoiceMonth, 1) <=
new DateTime(DateTo.InvoiceYear, DateTo.InvoiceMonth, 1)));
How to use Linq
A table
Field Birthday
Linq searches for recent birthdays (within 15 days)
from a in Employee where a.PositionStatus == true select new{ a.Name,a.Birthday}
Try below query
var fromdate = DateTime.Now;
var todate = DateTime.Now.AddDays(15);
var result =
(
from a in Employee
where a.PositionStuats==true && a.DateOfBirth.Value.Month >= fromdate.Month &&
a.DateOfBirth.Value.Month <= todate.Month &&
a.DateOfBirth.Value.Day >= fromdate.Day && a.DateOfBirth.Value.Day <= todate.Day
select new{ a.Name,a.Birthday}
).ToList();
Depending on your entity frame work version you can also replace a.DateOfBirth.Value.Month with a.DateOfBirth.Month.
I'm trying to create a basic room availability statement to use with linq to entity framework. I have two tables: 'Room' including columns RoomID/RoomSize and 'Booking' including BookingID/RoomID/StartDate/Enddate.
I have got a working sql statement:
SELECT RoomID, RoomSize from Room
where RoomID NOT IN (SELECT RoomID from booking
where ('08/01/2015' >= [start] AND '08/01/2015' <= [end]) OR ('08/20/2015' >= [start] AND '08/20/2015' <= [end]))
I have got this far with the linq to entity statement:
var rooms = (from r in db.Rooms
where !(((from b in db.Bookings
where (startDate >= b.StartDate && endDate <= b.EndDate) || (endDate >= b.StartDate && endDate <= b.EndDate)).Contains(r.RoomID))
select new AvailableRoom
{
ID = r.RoomID,
Size = r.RoomSize
});
I get an error at the last bracket before .Contains(r.RoomID) saying I should have a select statement but I just can't seem to get it working.
Any suggestions would be welcome.
If you reckon using lambdas would be better/easier please feel free to suggest and example. I'm just not too familiar with them myself.. yet.
Thank you.
You can use LINQ !...Any() for the SQL NOT IN(), like so :
var rooms = (from r in db.Rooms
where !db.Bookings
.Where(b => (startDate >= b.StartDate && endDate <= b.EndDate)
||
(endDate >= b.StartDate && endDate <= b.EndDate)
)
.Any(b => b.RoomID == r.RoomID)
select new AvailableRoom
{
ID = r.RoomID,
Size = r.RoomSize
});
What I am trying to do is search my database (ado.net with linq to sql) for all entrys within the last week.
The database has a field called date and I need the entries that are 7 days previous to the current date.
var records = (from r in context.PumpInfoTables
where r.Date.Equals(DateTime.Now)
//&& where r.date <= 7 days <--help
select r);
so I am looking for some help with this query, is there a between function that can be used?
DateTime weekFromNow = DateTime.Now.AddDays(-7);
var records = (from r in context.PumpInfoTables
where r.Date < DateTime.Now
&& r.Date >= weekFromNow
select r);
This is really simple:
var oneWeekAgo = DateTime.Today.AddDays(-7);
var records = (from r in context.PumpInfoTables
where r.Date >= oneWeekAgo
select r);
This assumes that you don't have entries from the future. If you do have, use this instead:
var today = DateTime.Today;
var oneWeekAgo = today.AddDays(-7);
var records = (from r in context.PumpInfoTables
where r.Date >= oneWeekAgo && r.Date <= today + 1
select r);
Please note:
My code uses DateTime.Today instead of DateTime.Now, assuming you are only interested in the date part, not the time.
Something like this?:
var records = (from r in context.PumpInfoTables
where r.Date > DateTime.Today.AddDays(-7)
select r);
I am working on a LINQ query to retrieve all records from the current week, however, I need to exclude any records from today and yesterday.
Here is what I have so far:
DateTime startThisWeek = DateFunctions.GetFirstDayOfWeek(DateTime.Now).AddDays(1);
DateTime endOfThisWeek = startThisWeek.AddDays(6);
DateTime today = DateTime.Now;
DateTime yesterday = DateTime.Now.AddDays(-1);
var notificationList =
(from n in db.DashboardNotifications
.OrderByDescending(n => n.NotificationDateTime)
where (n.NotificationDateTime >= startThisWeek &&
n.NotificationDateTime <= endOfThisWeek) &&
(n.NotificationDateTime != today &&
n.NotificationDateTime != yesterday)
select n).ToList();
The problem with above query is that it is not returning proper records , it also showing todays records too.
Assume your DateFunctions.GetFirstDayOfWeek works correctly
DateTime startThisWeek = DateFunctions.GetFirstDayOfWeek(DateTime.Now);
DateTime yesterday = DateTime.Today.AddDays(-1);
var notificationList =
(from n in db.DashboardNotifications
where n.NotificationDateTime.Date >= startThisWeek.Date &&
n.NotificationDateTime.Date < yesterday)
orderby n.NotificationDateTime descending
select n).ToList();
Comments: If start of current week is not before yesterday, then you will simply get no records. Otherwise yesterday always will be before current week end.
How to get start of week correctly:
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime date,
DayOfWeek startOfWeek = DayOfWeek.Monday)
{
DateTime result = date;
while (result.DayOfWeek != startOfWeek)
result = date.AddDays(-1);
return result.Date;
}
}
You are only excluding records for today and yesterday if they have the same time as when you run the report.
Try
DateTime startThisWeek = DateFunctions.GetFirstDayOfWeek(DateTime.Now.Date).AddDays(1);
DateTime endOfThisWeek = startThisWeek.AddDays(6);
DateTime today = DateTime.Now.Date;
DateTime yesterday = DateTime.Now.Date.AddDays(-1);
var notificationList =
(from n in db.DashboardNotifications
.OrderByDescending(n => n.NotificationDateTime)
where (n.NotificationDateTime >= startThisWeek &&
n.NotificationDateTime.Date <= endOfThisWeek) &&
(n.NotificationDateTime.Date != today &&
n.NotificationDateTime.Date != yesterday)
select n).ToList();
This is assuming that it is possible to have future notifications.
Ps, I'm not sure what the DateFunctions.GetFirstDayOfWeek method does nor why you are adding 1 day to it.