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)));
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.
how to write a linq query with to get records between 9 am 5 pm only. the records beyond that should be discarded.
timestamp datatype
code
var items = Pirs.Where(a => !a.dataFrame.EndsWith("AAAAAAAAAAA=") && (fromDate == null || fromDate.Value.Date <= TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(a.timestamp), TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).Date) && (toDate == null || toDate.Value.Date >= TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(a.timestamp), TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).Date))
.GroupBy(a => a.dataFrame.Substring(a.dataFrame.Length - 12))
.Select(g => g.First()).OrderBy(a => a.timestamp);
Pirs.Where(a.timestamp.TimeOfDay > new TimeStamp(9, 0, 0) && //all times after 9am
a.timestamp.TimeOfDay < new TimeStamp(17, 0, 0) && //all times before 5pm
a.timestamp.Date > fromDate && //all dates after fromData
a.timestamp.Date < toDate) //all dates before toDate
Do the following in where condition
TimeSpan span = TimeSpan.Parse("09:00:00");
TimeSpan espan = TimeSpan.Parse("17:00:00");
Pirs.Where(a => a.timestamp >= startDate && a.timestamp <= toDate && a.timestamp.TimeOfDay >= span && a.timestamp.TimeOfDay <= espan);
I have list of events, each event has two dates; start date and end date. I want to create a filter by months. How do I return dates that ranges between a month that a user selects?
for example, lets say the user selects month October, I want to return all events that are within this month.
I have used this to get the dates that ranges between todays date but now stuck on how to get the range between a month.
DateTime dateToCheck = DateTime.Today.Date;
DateTime startDate = DateTime.Parse(item["Start Time"].ToString());
DateTime endDate = DateTime.Parse(item["End Time"].ToString());
foreach (SPListItem item in collection)
{
if (startDate <= dateToCheck && dateToCheck < endDate)
{
ListBox1.Items.Add(item["EventTitle"].ToString());
}
}
// set up dummy data
var dates = new[] {DateTime.Now, DateTime.Now, DateTime.Now};
int month = GetMonth();
// get result
var result = dates.Where(date => date.Month == month);
EDIT: if you need to make sure the dates have the correct year as well, use
var dates = new[] {DateTime.Now, DateTime.Now, DateTime.Now};
int year = GetYear();
int month = GetMonth();
var result = dates.Where(date => date.Year == year && date.Month == month);
Of course, you can get the year/month numbers as well as the date-list from wherever.
EDIT2: if you get a DateTime object as input modify accordingly:
var dates = new[] {DateTime.Now, DateTime.Now, DateTime.Now};
var input = GetDateTime();
var result = dates.Where(date => date.Year == input.Year && date.Month == input.Month);
You still can use your code with little modifications. As start date you have to select 00:00 time of 1st of the month and as end date you have to use 00:00 time of 1st of the next month. In case of October 2015 it would be: 1 Oct 2015 <= date < 1 Nov 2015.
int year = 2015;
int month = 10;
DateTime dateToCheck = DateTime.Today.Date;
DateTime startDate = new DateTime(year, month, 1);
DateTime endDate = startDate.AddMonths(1);
foreach (SPListItem item in collection)
{
if (startDate <= dateToCheck && dateToCheck < endDate)
{
ListBox1.Items.Add(item["EventTitle"].ToString());
}
}
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)
I've found every DateTime format you can think of except the one I need: yyyymm. How do I go about getting this?
I have the following code I'm working with:
List <string> routines = new List<string>();
routines.Add("1 - Routine");
routines.Add("6 - Planned Outage");
int year = 2011;
int quarter = 1;
DateTime timeframe = new DateTime(year, (quarter * 3), 01).AddMonths(1);
var results =
(from wo in WORKORDERs
join wot in WORKORDERTYPEs on wo.Wot_oi equals wot.Wotyoi
join pri in PRIORITies on wo.Prio_oi equals pri.Priooi
join s in SITEs on wo.BEparn_oi equals s.Siteoi
where wo.Audt_created_dttm.Value.Year >= year - 3 && wo.Audt_created_dttm.Value.Year >= 2006
&& wo.Audt_created_dttm < timeframe && (s.Id =="NM" || s.Id == "TH") &&
!wot.Id.Contains("stand") && !(wo.Ci_cnc_date != null && pri.Prioid != "1 - Routine" &&
pri.Prioid != "6 - Planned Outage") && (SqlMethods.Like(pri.Prioid, "1%") ||
SqlMethods.Like(pri.Prioid, "2%") || SqlMethods.Like(pri.Prioid, "3%"))
select new {PM = wo.Wosource, Site = s.Id, Priority = pri.Prioid, Worktype = wot.Id,
WoNumber = wo.Id, Description = wo.Aenm, CreateDate = wo.Audt_created_dttm,
CloseDate = wo.Clsdt_date,
MonthNum = String.Format("{0:yyyy mm}", wo.Clsdt_date),
Planning = routines.Contains(pri.Prioid) ? "Planned" : "Unplanned"});
I tried using:
MonthNum = String.Format("{0:yyyy mm}", wo.Clsdt_date)
and that gives me the year correctly but the months are all 00. I'm guessing this is because the format for wo.Clsdt_date is different than my string format (it's mm/dd/yyyy).
Any ideas?
You need MM, not mm. mm is minutes.
"mm" is for minutes, "MM" is for Month, but with 2 digits of month. So in June 2012 you will have 201206, however if you need only one digit for month, you can use one "M" instead "yyyyM" and you will get 20126
DateTime.Now.ToString("yyyyMM")
In date formatting, mm means minutes, not months. Use MM instead.