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);
Related
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.
currently in the code I am outputting the employees table which have end dates of anything from before today's date and up to 90 days. I am trying to make it so only employees with maximum -30 days and + 90 of today's date.
(Fairly new so be easy)
(NOT SURE HOW TO CORRECTLY USE 2 DATES FOR .ADDDAYS )
Thanks
public ActionResult Index()
{
var employees = db.employees;
var today = DateTime.Today.AddDays(90);
var past = DateTime.Today.AddDays(-30);
var q = db.employees.Where(t => t.EndDate <= today );
return View(q.OrderByDescending(t => t.EndDate));
}
Why can't you use a AND (&&) condition like
var q = db.employees.Where(t => t.EndDate <= today && t.EndDate >= past);
It is simple by using conditional-AND && operator & combining both queries as single line:
var q = db.employees.Where(t => t.EndDate >= past && t.EndDate <= today)
.OrderByDescending(t => t.EndDate);
return View(q);
Try following code. You can add the OrderByDescending(t => t.EndDate) on the first line of code.
var q = db.employees.Where(t => t.EndDate >= past && t.EndDate<= today)OrderByDescending(t => t.EndDate);
return view(q);
Wish to convert this to LINQ.
select COUNT(*) as 'BillsOver30' from pssuite_web.pujhaccd
where billdays>30 and DATEDIFF(month,'07-07-2016', GETDATE()) <= 13
Group By Month(billdate)
This will show 13 rolling months from 07-07 with how many bills were over 30 for each month.
This is the terrible query I've written in LINQ, which doesn't work:
DateTime earliestDate = objdate1.DateStart.Value.AddMonths(-13);
var custQuery9 = ((from m in DataContext.pujhaccds
where m.billdays > 30
&& m.billdate >= earliestDate
&& m.billdate <= objdate1.DateStart
group m by m.billdays into p
select p)).Count();
To get result as you described in comments:
var result = (from m in DataContext.pujhaccds
where m.billdays > 30 &&
m.billdate >= earliestDate &&
m.billdate <= objdate1.DateStart
group m by m.billdate.Month into p
select new { Month = p.Key, Count = p.Count() }).ToList();
To get it like in original question replace the select above like this:
select p.Count()
That grouping will be problematic though if you have data from different years and want to separate it. If that is the case group by 2 fields - year and month:
var result = (from m in DataContext.pujhaccds
where m.billdays > 30 &&
m.billdate >= earliestDate &&
m.billdate <= objdate1.DateStart
group m by new { m.billdate.Value.Month, m.billdate.Value.Year } into p
select new { Date = $"{p.Key.Month} - {p.Key.Year}", Count = p.Count() }).ToList();
Trying to get only Thursdays of 1 year back
using (var context = new Context1())
{
// Get 1 year back only Thursday
var oneYearEarlier = DateTime.Now.AddYears(-1);
var query = (from c in context.Daily25Data
where c.Date > oneYearEarlier && c.Date.DayOfWeek == DayOfWeek.Thursday
select c).ToList();
Console.WriteLine(query);
}
and getting
Additional information: The specified type member 'DayOfWeek' is not
supported in LINQ to Entities. Only initializers, entity members, and
entity navigation properties are supported.
This is a clever solution using EntityFunctions.DiffDays
from his post:
DateTime firstSunday = new DateTime(1753, 1, 7);
var bookings = from b in this.db.Bookings
where EntityFunctions.DiffDays(firstSunday, b.StartDateTime) % 7 == 1
select b;
LINQ will not recognize the second part of your query into SQL. You need to break your query up in order to perform the filter.
using (var context = new algoventurelab_db1Context())
{
// Go one year back from current date
var startDate = DateTime.Today.AddYears(-1);
// This will get all dates in context greater than start date.
var query1 = (from c in context.Daily25Data
where c.Date >= startDate
select c).AsEnumerable();
//this will filter only thurdays
var query = from c in query1
where c.Date.DayOfWeek == DayOfWeek.Thursday
select c).ToList();
Console.WriteLine(query);
}
My query is like
var TMJM = (from TT in r2ge.Transcription_Tracker
join TMA in r2ge.Transcription_Master on TT.Transcription_Id equals TMA.Transcription_Id
select new
{
Trans_Id = TMA.Transcription_Id,
Modified_dtm = TMA.Modified_dtm
}).Distinct();
var qq = TMJM.Where(dr => (dr.Modified_dtm.Date >= sev_back_datetime) && (dr.Modified_dtm.Date <= DateTime.Now.Date));
It gives output Enumeration yielded no results while Data is there
But when I separate start date and end date then It will show date but when I combine both start and end date then It won't give output..
Any mistake in query?
Evaluating each part of the date could be fraught with issue. I am assuming you are doing this to remove the time portion which can sometimes be problematic. Subsequently you can just evaluate the 'date' portion of the date time variable. This little example should explain.
class Program
{
static void Main(string[] args)
{
List<DateTime> dates = new List<DateTime>();
dates.Add(DateTime.Now.AddDays(-50));
dates.Add(DateTime.Now.AddDays(-51));
dates.Add(DateTime.Now.AddDays(-52));
dates.Add(DateTime.Now.AddDays(-53));
dates.Add(DateTime.Now.AddDays(-99));
dates.Add(DateTime.Now.AddDays(-100));
dates.Add(DateTime.Now.AddDays(-101));
var sev_back_datetime = DateTime.Now.AddDays(-100);
Console.WriteLine(sev_back_datetime.Date.ToShortDateString());
Console.WriteLine("---------------------");
var query = from date in dates
where date.Date >= sev_back_datetime.Date &&
date.Date <= DateTime.Now.Date
select date;
foreach (var date in query.ToList())
Console.WriteLine(date.ToShortDateString());
Console.ReadLine();
}
}
EDIT - a direct example for you.
var TMJM = (from TT in r2ge.Transcription_Tracker
join TMA in r2ge.Transcription_Master on TT.Transcription_Id equals TMA.Transcription_Id
select new
{
Trans_Id = TMA.Transcription_Id,
Modified_dtm = TMA.Modified_dtm
}).Distinct();
var qq = TMJM.Where(dr => (EntityFunctions.TruncateTime(dr.Modified_dtm) >= EntityFunctions.TruncateTime(sev_back_datetime)) && (EntityFunctions.TruncateTime(dr.Modified_dtm) <= EntityFunctions.TruncateTime(DateTime.Now)));