Steven Zack
Steven Zack

Reputation: 5104

linq: how to get records update within 1 minute?

I have a table which has a column 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;

Upvotes: 0

Views: 241

Answers (2)

Matt
Matt

Reputation: 1431

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.

  1. First I created a console application as I wasn't clear what you were using (assuming wpf or silverlight)
  2. 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()
    
        {
        }
    {
    
  3. 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();
    }
    
  4. Result should be "Test3"

Cheers

Matt

Upvotes: 0

Quintin Robinson
Quintin Robinson

Reputation: 82325

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;

Upvotes: 3

Related Questions