2

I'm trying to wrap my head around this "new concept" called LINQ. Ever heard of it? LOL

Anyway, I've read enough to know this can be written much clearer using LINQ, but I don't understand how I'd write it:

DataTable table = MySqlDateTimeQueryResult();
for (int i = 0; i < table.Rows.Count; i++) {
  DataRow r = table.Rows[i];
  DateTime dt = (DateTime)r["Date_Time"];
  Console.WriteLine(dt);
}

EDIT [Title Change]: How would I write this using LINQ?

Apparently, this is not a query.

  • 2
    I'm confused, what are you trying to query? This looks like a loop, not a search. – Lucas B Jun 01 '11 at 14:19
  • LOL. Maybe I'm confused, too. I *think* I'm querying the date. Like I said, `New to LINQ.` I could be completely off base. –  Jun 01 '11 at 14:22

5 Answers5

5

This will return an IEnumerable<DateTime> with all elements of the Date_Time column:

    table.Rows.Select(r=>((DateTime)r["Date_Time"]));
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
  • The `Console.WriteLine()` is academic. I need to extract the "Date_Time" field. –  Jun 01 '11 at 14:27
3

In this specific case, LINQ might not actually be beneficial. See this post by Eric Lippert and the discussion on this previous SO question.

This is how you would do it, though.

table.Rows                       // Start with an IEnumerable
    .Select(r => r["Date_Time"]) // Map each item to something, in this case extract the value
    .ToList()                    // ForEach comes off of List, not IEnumerable
    .ForEach(x => Console.WriteLine(x)); // Do something with each
Community
  • 1
  • 1
David Ruttka
  • 14,269
  • 2
  • 44
  • 39
  • [+1] I like how you've broken this down into the parts that each is doing. The `Console.WriteLine()` is academic. I need to extract the "Date_Time" field. –  Jun 01 '11 at 14:29
  • 1
    @jp2code Then just stop after the `.Select`, which has done the extraction to an `IEnumerable`. You could call `ToArray()` instead of `ToList()` if you want them that way. – David Ruttka Jun 01 '11 at 14:30
  • 1
    @jp2code you can do the `(DateTime)` cast inside the lambda too. `.Select(r => (DateTime)r["Date_Time"])` – David Ruttka Jun 01 '11 at 14:32
2
foreach(DateTime dt in MySqlDateTimeQueryResult().AsEnumerable()
                            .Select(row => row.Field<DateTime>("Date_Time")))
    Console.WriteLine(dt);
manji
  • 47,442
  • 5
  • 96
  • 103
2

I'm not sure you really need LINQ in this case, but if you really want to express what you are doing using LINQ, try this:

var query = from  DataRow r in table.Rows select r["Date"];

 foreach (var q in query)
 {
     Console.WriteLine(q.ToString());
 }
InBetween
  • 32,319
  • 3
  • 50
  • 90
  • Could my first line be written as `foreach (var q in from DataRow r in table.Rows select r["Date_Time"])`? I would test this directly myself, but my DEBUGGER is deep in a piece of code that's been giving me errors, and I hate to exit right now. –  Jun 01 '11 at 14:39
0
 var dates = table.AsEnumerable().Select(r => r["Date_Time"]);
Mike
  • 2,912
  • 4
  • 31
  • 52