简体   繁体   English

c#在.NET 2上使用DataTable AsEnumerable()

[英]c# using DataTable AsEnumerable() on .NET 2

I am trying to run the following code on a .net 2 winforms app: 我试图在.net 2 winforms应用程序上运行以下代码:

DataTable dt = this.GetData(null, null, true, sql);

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

I am getting errors for the "using system.linq" and the ".AsEnumerable()". 我收到“using system.linq”和“.AsEnumerable()”的错误。 Is there any way I can resolve this to use AsEnumerable()? 有什么方法可以解决这个问题,使用AsEnumerable()? Or should I just abandon this method? 或者我应该放弃这种方法?

Thanks! 谢谢!

.NET 2 doesn't have LINQ. .NET 2 没有 LINQ。 You could use LINQBridge , which may or may not include the AsEnumerable() extension method for DataTable . 您可以使用LINQBridge ,它可能包含也可能不包含DataTableAsEnumerable()扩展方法。 If it does, you can just use Cast<DataRow>() instead, optionally via an explicitly typed range variable: 如果是,您可以使用Cast<DataRow>() ,可选地通过显式类型的范围变量:

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

You'd then also need the Field<T> extension method on DataRow . 然后,您需要DataRow上的Field<T>扩展方法。 You could probably write that yourself though, if it's not part of LINQBridge. 如果它不是LINQBridge的一部分,你可以自己写一下。

Just to make it clear - none of this will work pleasantly if you're also using Visual Studio 2005, because you need the C# 3 features of lambda expressions, extension methods etc. 只是要清楚- 这些都不能正常工作愉快,如果您还使用Visual Studio 2005中,因为你需要的C#lambda表达式,扩展方法等3特点

Is there any possibility you could upgrade to .NET 3.5? 有没有可能升级到.NET 3.5? It would make life a lot easier... 它会让生活变得更轻松......

LINQ是在.NET 3.5中引入的,所以我担心你在这里运气不好:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM