linq: how to get records update within 1 minute?
I have a table which has a col开发者_如何转开发umn called lastUpdateTime, datetime type. How to do a query that will list all records that been updated within last 1 minute?
DataContext dc=new DataContext();
from a in dc.Acccounts
where a.lastUpdateTime //how to write this line?
select a;
Well this might be dependent on timezone data that can get tricky but assuming simplicity you can do..
from a in dc.Accounts
where a.lastUpdateTime >= DateTime.Now.AddMinutes(-1)
select a;
I am sure there are about 900 ways to skins this cat. I am making a lot of assumptions here but this should get you running.
- First I created a console application as I wasn't clear what you were using (assuming wpf or silverlight)
Created a person class with your LastUpdateDate:
public class Person { public int ID { get; set; } public string FirstName { get; set; } public DateTime LastUpdateDate { get; set; } public Person() { } {
Populated a People list with arbitrary data. Peformed the query using a lambda expression. Look up Timespan if the subtraction isn't making sense. Notice 2 of them have an update date of greater than 1 minute ago.
static void Main(string[] args) { List<Person> People = new List<Person>(); People.Add(new Person() { ID = 1, FirstName = "Test1", LastUpdateDate = DateTime.Now.AddMinutes(-10) }); People.Add(new Person() { ID = 2, FirstName = "Test2", LastUpdateDate = DateTime.Now.AddMinutes(-5) }); People.Add(new Person() { ID = 3, FirstName = "Test3", LastUpdateDate = DateTime.Now }); var result = People.Where(p => (DateTime.Now - p.LastUpdateDate).Minutes <= 1); foreach (Person p in result) { Console.WriteLine(p.FirstName); } Console.ReadLine(); }
Result should be "Test3"
Cheers
Matt
精彩评论