-1

(I am using LINQ to SQL for retrieving the table from database)

I need to loop through a Database table(the has a field Data) to mach the current date with the date in table.How can i achieve this?

user2536023
  • 165
  • 1
  • 4
  • 14
  • http://msdn.microsoft.com/en-us/library/bb546192(v=vs.110).aspx What have you tried, where do you get stuck? Yuo can use a foreach over the iQueryable, or directly select the correct record? – oerkelens Nov 12 '13 at 12:38

1 Answers1

0

Don't use a loop - this will be extremely slow if the table is large. You want to do this in a set based fashion - the IQueryable .Where() method, i.e:

MyTable.Where(x => x.SomeDate == DateTime.Today);

This assumes the dates in the DB store the date part only. If the time part is stored in the DB and you want to ignore that in the comparison, you need to do something like:

MyTable.Where(x => x.SomeDate >= DateTime.Today && x.SomeDate < DateTime.Today.AddDays(1));
Ben
  • 1,913
  • 15
  • 16
  • 2
    Also depending on the type of date time column, the end of the second example may need to subtract a few milliseconds to avoid bringing in rows that are not wanted. http://stackoverflow.com/q/715432/16391 – StingyJack Nov 12 '13 at 12:53