how to convert string to datetime in Linq where clause - c#

DateTime newDate = DateTime.Now.AddDays(-30);
var queryResultPage = (from r in CustomPlanning
where (r.Assignment == "In Use") &&
(r.CUST_SHRT_NM != null) &&
System.Convert.ToDateTime(r.lastlogontimestamp) < newDate &&
!(from uA in SurveyActivity
where uA.CustomDemandID == r.ID
select uA.CustomDemandID).Contains(r.ID)
select r)
.OrderBy(t => t.ID);
Above is my code. lastlogontimestamp is a string field in my table. i need to do this check to show the query result in a grid. Can someone please help?

ok i tried this and it worked
DateTime newDate = DateTime.Now.AddDays(-30);
var queryResultPage = (from r in CustomPlanning
where (r.Assignment == "In Use") && (r.lastlogontimestamp != null && r.lastlogontimestamp != string.Empty)
&& (r.CUST_SHRT_NM != null)
&& !(from uA in SurveyActivity where uA.CustomDemandID == r.ID select uA.CustomDemandID).Contains(r.ID)
select r).OrderBy(t => t.ID).ToList();
queryResultPage = queryResultPage.Where(r => System.Convert.ToDateTime(r.lastlogontimestamp) < newDate).ToList();

You cannot in this query directly. You need to materialize the results from the query in a list, and run another query to filter the returned CustomPlanning with Linq-To-Objects (it supports conversion).
You can convert newDate to string
string newDate = DateTime.Now.AddDays(-30).ToString();
and try to compare strings instead, but you need to cover lots and lots of cases, so it is not recommended. You can consider changing the type of lastlogontimestamp to DateTime, when you have dates, it is natural to store them as dates.

Related

Entity Framework DateTIme Query

I'm having an issue with a linq subquery return invalid data when adding in datetime checks as part of the where clause.
This is the original query and it is returning 0; because the result set is null
var subquery =
(from item in g
from e in item.Entry
where e.Type == 1
&& e.EntryType == 2
&& item.StartDate >= priorMonthStartOfDay
&& item.EndDate <= startOfDayQueryParam
select e.Amount).Sum() ?? 0M;
I modified the query to see what the data was; here is that query and the resulting dataset.
var subquery =
(from item in g
from e in item.Entry
where e.Type == 1
&& e.EntryType == 2
select new
{
Amount = e.Amount,
SD = item.StartDate,
ED = item.EndDate,
QD = priorMonthStartOfDay
};
So then I added in the start date comparison and the results are below. The priorMonthStartOfDay is a DateTime with a value of 12/1/2015 12:00:00 AM
var subquery =
(from item in g
from e in item.Entry
where e.Type == 1
&& e.EntryType == 2
&& item.StartDate >= priorMonthStartOfDay
select new
{
Amount = e.Amount,
SD = item.StartDate,
ED = item.EndDate,
QD = priorMonthStartOfDay
};
Why is the date comparison not behaving as I would expected? Given the value of priorMonthStartOfDay, I would expect the result set to be the same for the last two queries. I'm guessing it has something to do with the time equal comparison because if I subtract a second from the priorMonthStartOfDay then the result sets match up again.
The only logical explanation could be that your priorMonthStartOfDay and/or startOfDayQueryParam variables contain time part not shown in the debugger. Note that by default milliseconds part is not shown, not to mention ticks.
To be 100% sure you are comparing against dates, change the date part of the criteria to
&& item.StartDate >= priorMonthStartOfDay.Date
&& item.EndDate <= startOfDayQueryParam.Date

Can't compare dates using SQLite with EF6

I'm trying to compare dates using Linq to Entities on a SQLite database. The following code works, but I need to trim off the time portion to get the correct results.
return (from c in Context.Car
join d in Context.Driver on c.CarID equals d.DriverID
join r in Context.Rides on c.CarID equals r.RideID into rideJoin
from rides in rideJoin.DefaultIfEmpty()
where c.IsActive && d.IsActive
group rides by new { c.CarID, d.FullName, d.HireDate, d.FirstPayRiseDate } into grp
select new MyCustomClass
{
CarID = grp.Key.CarID,
Driver = grp.Key.FullName,
NumberOfRides = grp.Count(x => x != null && x.RideDate >= grp.Key.HireDate && x.RideDate <= grp.Key.FirstPayRiseDate)
}).OrderBy(x => x.Driver ).ToList();
I've tried using System.Data.Entity.DBFunctions like so and I get this error:
NumberOfRides = grp.Count(x => x != null && DbFunctions.TruncateTime(x.RideDate) >= grp.Key.HireDate && DbFunctions.TruncateTime(x.RideDate) <= grp.Key.FirstPayRiseDate)
SQL logic error or missing database no such function: TruncateTime
I also get the same error with DBFunctions.DiffDays()
I've also tried casting to Date like so and get this error:
NumberOfRides = grp.Count(x => x != null && x.RideDate.Date >= grp.Key.HireDate && x.RideDate.Date <= grp.Key.FirstPayRiseDate)
'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported
What gives? How am I supposed to do Date functions in Linq to Entities with SQLite??
I need to trim off the time portion to get the correct results
No you don't. If you want to include the rows from startDate through endDate inclusive then just use
... && x.RideDate >= startDate && x.RideDate < endDate.AddDays(1)
(Note that the second comparison is now "strictly less than".)
How are you storing dates on the database ? as unix time integrs ?
in that acse you can amend your connection string to include this following config setting and it will make it easy to read the datetime value via EF.
datetimeformat=UnixEpoch;datetimekind=Utc
So something like :
data source="|DataDirectory|\data.sqlite";datetimeformat=UnixEpoch;datetimekind=Utc
Ref: https://stackoverflow.com/a/24323591/3660930

List.contains throwing null exception?

I have a join query like
public ActionResult Import(int[] userValue, int[] provalue, DateTime? StartDate, DateTime? EndDate)
{
List<int> actallid = db.Activitys.Select(x=>x.ID).ToList();
List<int> actchild = FileOperation.ChildActivity(actallid);
DateTime Start_Date = Convert.ToDateTime(StartDate);
DateTime End_Date = Convert.ToDateTime(EndDate);
IEnumerable<ViewModelActivitySearch> search = from r in db.Reports
join a in db.Activitys on r.ID equals a.Report_ID
join u in db.Users on r.Users_ID equals u.ID
join p in db.Projects on a.Projects_ID equals p.ID
join act in db.ActivityTypes on a.Activity_Type_ID equals act.ID
where (
(r.Start_Date == StartDate || r.End_Date == EndDate || userValue.Contains((int)r.Users_ID) || provalue.Contains((int)a.Projects_ID))
&&
//status is submitted=2, approved=3,exclude child activities
(!actchild.Contains((int)a.ID))
&&
(r.Status == 2 || r.Status == 3)
)
select new ViewModelActivitySearch
{
Id = a.ID,
Activity_Text = a.Activity_Text,
Deliverable = a.Deliverable,
Employee = string.Concat(u.FirstName, " ", u.LastName),
Start_Date = r.Start_Date,
End_Date = r.End_Date,
Activity_Date = a.Activity_Date,
Project = p.Name,
category = act.Activity_Type
};
BindProjectAndUser();
return View(search.ToList());
}
I have a search page where the user can enter a start date,enddate,choose users and choose projects.
I then made a join query to fetch all the data according to that variables.
The problem is when the user doesnot choose any user or project
the code
userValue.Contains((int)r.Users_ID) || provalue.Contains((int)a.Projects_ID))
gives the error
Unable to create a null constant value of type 'System.Int32[]'. Only entity types, enumeration types or primitive types are supported in this context.
What else can I use instead of contains so that this error does not arises.
Thanks
Check for null in values that you try to cast explicitly to int (like r.Users_ID) with ternary operator before using Contains:
where (r.Start_Date == StartDate || r.End_Date == EndDate ||
r.Users_ID != null? userValue.Contains((int)r.Users_ID) : false ||
a.Projects_ID != null ?provalue.Contains((int)a.Projects_ID) : false)
Method Contains() is a method to check if there's a certain value in a list of values of same type and it's perfectly fine to use it here.
Always do server-side validation!
Especially if you're using the users input for a SQL statement it's very important to validate it (sql injection)!
Simplest way: if-statements
if(userinput == null){
return View();
}
Note: If you are using Convert.ToDateTime(), you won't get an exception if the provided DateTime is null. You'll get the DateTime.MinValue and this could end in unexpected results.

Short Date in Linq

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

casting to datetime data to month and year in C#

i have linq to entities query below but i want the where clause to use the logic in sample T-SQL code after it.
var myList = from p in ctx.myTable
where !ctx.Report.Any(m => m.ReportDate == DateTime.Today && m.ReportDate == DateTime.Today)
select p;
How can i compare to month and year in my where clause like in the T-SQL below?
WHERE month(R.ReportDate) = month(GETDATE()) AND YEAR(R.ReportDate) = YEAR(GETDATE()))
Dealing with dates in LINQ to Entities is a little tricky. I'm not sure you can generate MONTH() or YEAR() within your SQL. But you can definitely generate DATEPART call with proper m or y as datepart.
Use SqlFunctions.DatePart Method (String, String) to do that:
var todayMonth = DateTime.Today.Month;
var todayYear = DateTime.Today.Year;
var myList = from p in ctx.myTable
where !ctx.Report.Any(m => SqlFunctions.DatePart("m", m.ReportDate) == todayMonth && SqlFunctions.DatePart("y", m.ReportDate) == todayYear)
select p;
Should generate
WHERE DATEPART(m, R.ReportDate) = 2 AND DATEPART(y, R.ReportDate) = 2014)
If you really want the GETDATE() part, you can use following:
var myList = from p in ctx.myTable
where !ctx.Report.Any(m => SqlFunctions.DatePart("m", m.ReportDate) == SqlFunctions.DatePart("m", SqlFunctions.GetDate()) && SqlFunctions.DatePart("y", m.ReportDate) == SqlFunctions.DatePart("y", SqlFunctions.GetDate()))
select p;
Update
I just found that page: CLR Method to Canonical Function Mapping which states, that DateTime.Month and DateTime.Year properties are translated into MONTH and YEAR methods, so following should work just fine:
var myList = from p in ctx.myTable
where !ctx.Report.Any(m => m.ReportDate.Month == SqlFunctions.GetDate().Month && m.ReportDate.Year == SqlFunctions.GetDate().Year)
select p;
A DateTime object has Month and Year property. You can make use of that.
DateTime dateTime = new DateTime();
if (dateTime.Month > DateTime.Today.Month) {
// Do something
}
if (dateTime.Year> DateTime.Today.Year)
{
// Do something
}
Try the Following Since DateTime Contains Year and Month.
where !ctx.Report.Any(m => m.ReportDate.Month == DateTime.Today.Month && m.ReportDate.Year == DateTime.Today.Year)

Categories