Greetings
I have the personal data of an individual including birth date
How do I get the information of people born between the date 1 and date 2?
date of birth is in a SQL Server database Compact
I get a record this way
using (ISession session = NHibernateConfiguration.OpenSession())
{
var production = session
.CreateCriteria(typeof(Person))
.Add(Restrictions.Eq("Date", date))
.List<Person>();
return production;
}
instead of .Add(Restrictions.Eq("Date", date)) use .Add(Restrictions.Between("Date", fromDate, toDate))
If you have these Object's Stored on a List than you could say ,
for(int i=0;i<list.count;i++)
{
if(list[i].getBirthdate.Day == 1 || list[i].getBirthdate.Day == 2 )
//DoSomething
}
But man ,you should be more Specific ,if these Information's are Stored in DataBase ,if Birth Date is Stored As Formated DateTime or whatever ,please be more specific and show us some code.
By the tags in your question, it looks like you're using Nhibernate. If you use Linq-to-Nhibernate, then you could use a linq statement like this:
var query = myISession.Linq<Person>();
var result = from entity in query
where entity.Dob >= dob1 && entity.Dob <= dob2
select entity;
return result.Count() > 0 ? result.ToList() : null;
I like extensions methods much more
var persons = GetPersonList();
var range = persons.Where(p => p.Dob >= startDate && p.Dob <= endDate);
Note that I know this is basically the same as using LINQ syntax, I just don't like linq syntax :).
Related
EDIT All the other answers that link to a previous question's wont work for me because they are either using two tables or they know what startdate they are looking for.
I have the following LINQ query where i am trying to get the data from a table for the last 14 days. LINQ does not recognize Convert.ToDatetime method. The Query_Date column is a type string and i can't change it. How do i get the data i need?
var logs = (from bwl in db.UserActivities
where Convert.ToDateTime(bwl.Query_Date) >= DateTime.Now.AddDays(-14)
select new
{
Id = bwl.Id,
UserEmail = bwl.UserEmail
}).ToList();
So i couldn't get what i wanted because i would have to make too many changes so the workaround i am doing is: I am using this code
var logs = db.UserActivities.ToList().Take(100);
This will get me the last 100 entries. I will give options for more or less entries then they can filter it on date in the search bar on the datatable.
It's not great but time is against me and this will suffice.
Do you have data being entered at least every day? If so, what about something like this:
var oldestDate = DateTime.Now.AddDays(-14);
var dateString = oldestDate.ToString(*/ your date format */);
var oldestID = db.UserActivities.First(b => b.Query_Date == dateString).Id;
var logs = (from bwl in db.UserActivities
where bwl.Id > oldestID
select new {
Id = bwl.Id,
UserEmail = bwl.UserEmail
}).ToList();
Basically you find a record on that date and use its ID as a proxy for the date. This only works in certain circumstances (i.e. if the IDs are in date order).
This also isn't guaranteed to be the oldest entry on that date, but that could be achieved either by using ID order:
var oldestID = db.UserActivities.Where(b => b.Query_Date == dateString)
.OrderBy(b => b.Id)
.First().Id;
Or more lazily by using -15 instead of -14 when you add days, if you don't mind grabbing an unknown percentage of that 15th day.
Convert.ToDateTime works if your Query_Date has proper format. If not, check your string expression is convertable format.
I tested below code and it works fine when I assume Query_Date is a form of DateTime.toString().
var logs = (from bwl in db.UserActivities
where DateTime.Now - Convert.ToDateTime(bwl.Query_Date) <= TimeSpan.FromDays(14)
select new
{
Id = bwl.Id,
UserEmail = bwl.UserEmail
}).ToList();
I also tested with your where expression Convert.ToDateTime(bwl.Query_Date) >= DateTime.Now.AddDays(-14) and confirmed that it gives same result.
My Table like this.
name date phonenumber
venky 25-06-2013 123123123
vasud 27-06-2013 2423727384
sdfds 14-06-2013 12312332132
If user want to see june month records then he pass 06 as input parameter how to write linq to sql query to get june month records as output..
Well it sounds like you just want something like:
public IQueryable<Record> GetRecordsForMonth(int month)
{
return new RecordContext().Where(record => record.Date.Month == month);
}
That's assuming your date field in the database is actually an appropriate datetime field or something similar. If it's not, then fix your schema.
Alternatively, for dates within a range, you could take two DateTime values in the method and filter that way:
public IQueryable<Record> GetRecordsForMonth(DateTime minDateInclusive.
DateTime maxDateExclusive)
{
return new RecordContext().Where(record => record.Date >= minDateInclusive
&& record.Date < maxDateExclusive);
}
I'm new to LINQ and LINQ to SQL and don't understand what's wrong with this code. The Excetpion.Message I get is
"Query operator 'Last' is not supported."
What I'm trying to do is get the earliest LastActivityUtcout of the latest 100. The code follows.
var postTimes = from post in db.Post
where post.LastActivityUtc != null
orderby post.LastActivityUtc descending
select post.LastActivityUtc;
DateTime startDate = DateTime.MinValue;
if (postTimes.Count() >= 2)
{
startDate = postTimes.Take(100).Last().Value;
}
Brandon has posted a solution, but it requires copying the whole list in memory.
If you just want to "transition" from database queries to in-process, you can use AsEnumerable:
startDate = postTimes.Take(100).AsEnumerable().Last().Value;
Having said that, you possibly do want to call ToList(), but earlier - to avoid having to execute the query once for the count, and once for the last value:
var postTimes = (from post in db.Post
where post.LastActivityUtc != null
orderby post.LastActivityUtc descending
select post.LastActivityUtc).Take(100).ToList();
DateTime startDate = DateTime.MinValue;
if (postTimes.Count >= 2)
{
startDate = postTimes.Last().Value;
}
That will execute the database query once, but only fetch the first 100 records into memory. Of course, it falls down somewhat if you were going to use postTimes elsewhere...
Call .ToList() on postTimes and then try using .Last()
startDate = postTimes.Take(100).ToList().Last().Value;
Please help.
I am trying to figure out how to use DATE or DATETIME for comparison in a linq query.
Example:
If I wanted all Employee names for those who started before today, I would do something like this in SQL:
SELECT EmployeeNameColumn
FROM EmployeeTable
WHERE StartDateColumn.Date <= GETDATE() //Today
But what about linq?
DateTime startDT = //Today
var EmployeeName =
from e in db.employee
where e.StartDateColumn <= startDT
The above WHERE doesn't work:
Exception Details: System.NotSupportedException: The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Use the class DbFunctions for trimming the time portion.
using System.Data.Entity;
var bla = (from log in context.Contacts
where DbFunctions.TruncateTime(log.ModifiedDate)
== DbFunctions.TruncateTime(today.Date)
select log).FirstOrDefault();
Source: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/84d4e18b-7545-419b-9826-53ff1a0e2a62/
That should work. Are you sure that there isn't another part of the query that triggered the exception? I have several instances of queries of the form
var query = from e in db.MyTable
where e.AsOfDate <= DateTime.Now.Date
select e;
in my code.
It may be due to the date in the database being nullable. Try this:
var EmployeeName =
from e in db.employee
where e.StartDateColumn.Value <= startDT
You can check the condition like this
var nextDay = DateTime.Today.AddDays(1);
var query = from e in db.MyTable
where e.AsOfDate >= DateTime.Today && e.AsOfDate < nextDay
select e;
here you'll get the records on AsOfDate date as we checking between today(00:00:00) and tommorow(00:00:00) we'll get today's date record only what ever may be the time...
You can not use .Date
If you would like to check for today you can create a datetime with no time
DateTime myDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
var e = (from mds in myEntities.Table
where mds.CreateDateTime >= myDate
select mds).FirstOrDefault();
try this:
DateTime dd = DateTime.Parse("08/13/2010 00:00:00");
var data = from n in ContributionEligibilities
where n.ModifiedDateTime.Date >= DateTime.Parse("08/13/2010").Date
select n;
data.Dump("Result") ;
I'm curious to the error message saying 'Date', when you're passing a 'DateTime'. Could it be that 'StartDateColumn' is actually a 'Date', rather than a 'DateTime' in the database? That might mess up the comparison...
use a local variable to store the Date value and then use that variable in the query:
DateTime today = DateTime.Now.Date;
from scheme in context.schemes
where scheme.EndDate > today
select scheme
I am using a LinqDataSource and I had problems getting my query with a Date Comparison in it to execute without errors. The answer is to use the WhereAddParameters function and add the test value as a strongly typed parameter.
See the example below where I am matching a groupid and checking to see if the StopDate in my record is greater that or equal to a Date/Time stamp of now.
I am using this code fragment currently and it works like a charm.
LinqCampaigns.WhereParameters.Add("StopDate", System.Data.DbType.Date, DateTime.Now.ToString())
LinqCampaigns.Where = "GroupId = " & myGrp & " && " & "StopDate >= #StopDate"
Works like a charm....
.Date did not work, but .Day did for me.
var query = from o in Payments
where o.Order.OrderDate.Day != o.PaymentDate.Day
orderby o.Order.OrderDate
select new
{
o.Order.OrderID,
o.Order.OrderDate,
o.PaymentDate,
o.Order.FirstName,
o.Order.LastName,
o.Order.CustomerID
};
query.Dump();
ensure that you check null value like this :
'(from mm in _db.Calls
where mm.Professionnal.ID.Equals(proid)
&& mm.ComposedDate.HasValue &&
(mm.ComposedDate.Value >= datemin) && (mm.ComposedDate.Value <= date)
select mm).ToArray();'
Anyone know how to query for a specific date within entity framework ? I tried following code, but it gives me NotSupportedException.
var deposit = (from tempDeposit in entities.Deposit
where !tempDeposit.IsApproved
&& tempDeposit.CreatedDate.Date == DateTime.Today
select tempDeposit).FirstOrDefault();
I also tried following code but still gave me NotSupportedException.
var deposit = (from tempDeposit in entities.Deposit
where !tempDeposit.IsApproved
&& tempDeposit.CreatedDate.ToFullString() == DateTime.Today.ToFullString()
select tempDeposit).FirstOrDefault();
ToFullString() is an extension method
public static string ToFullString(this DateTime date){
return date.ToString("yyyyMMdd");
}
Please help.
Try this:
var d1 = DateTime.Today;
var d2 = d1.AddDays(1);
var deposit = (from tempDeposit in entities.Deposit
where !tempDeposit.IsApproved
&& tempDeposit.CreatedDate >= d1
&& tempDeposit.CreatedDate < d2
select tempDeposit).FirstOrDefault();
Since you can't use the .Date property of a DateTime column, since that would have to be written in SQL with a range of converts, casts, truncates, etc. then you need to compare the whole DateTime value, date and time both, to a value, and thus you need to compare to a range.
Edit Changed to reflect that .AddDays isn't supported.
Well, after sometime tried, I found the solution
var deposit = entities1.Deposit
.Where("CreatedDate >= #0 and CreatedDate < #1", DateTime.Today, DateTime.Today.AddDays(1))
.FirstOrDefault();