C # using DataTable AsEnumerable () for .NET 2

I am trying to run the following code in a .net 2 winforms application:

DataTable dt = this.GetData(null, null, true, sql); DateTime minDate = (from f in dt.AsEnumerable() select f.Field<DateTime>("Timestamp")).Min(); 

I get errors for "using system.linq" and ".AsEnumerable ()". Is there a way to enable this to use AsEnumerable ()? Or should I just abandon this method?

Thanks!

+4
source share
2 answers

.NET 2 does not have LINQ. You can use LINQBridge , which may or may not include the AsEnumerable() extension method for the DataTable . If so, you can simply use Cast<DataRow>() instead, optionally via an explicit range variable:

 DateTime minDate = (from DataRow f in dt.AsEnumerable() select f.Field<DateTime>("Timestamp")).Min(); 

You will also need a method to extend Field<T> to a DataRow . You could probably write this yourself, though if it's not part of LINQBridge.

Just to make it clear - none of this will work nicely if you are also using Visual Studio 2005, because you need C # 3 functions for lambda expressions, extension methods, etc.

Is it possible to upgrade to .NET 3.5? That would make life so much easier ...

+5
source

LINQ was introduced in .NET 3.5, so I'm afraid you're out of luck here :(

0
source

Source: https://habr.com/ru/post/1341787/


All Articles