是否可以为非强类型数据表构建动态LINQ查询?(Is it possible to build dynamic LINQ queries to non

编程入门 行业动态 更新时间:2024-10-26 08:31:44
是否可以为非强类型数据表构建动态LINQ查询?(Is it possible to build dynamic LINQ queries to non-strongly type datatables?)

使用LINQ ,我一直在尝试使用System.Linq.Dynamic库来动态查询数据表。 问题是它不是强类型的,而select的扩展方法是期望IEnumerable作为源。

有办法解决这个问题吗? 示例代码:

var query = dataSet.Tables[0].AsEnumerable().Select("new(Regional)");

Using LINQ, I've been trying to use the System.Linq.Dynamic library in order to query a datatable dynamically. The problem is that it's not strongly typed, and the extension method for select is expecting an IEnumerable as the source.

Is there a way to work around this? Example code:

var query = dataSet.Tables[0].AsEnumerable().Select("new(Regional)");

最满意答案

AsEnumerable返回一个EnumerableRowCollection<DataRow> ,因此Select扩展方法应该可以正常处理AsEnumerable的结果...

例如 :

var query = dataSet.Tables[0].AsEnumerable().Select(row => row.Field<string>("Regional"));

I found a solution here. I know this approach is might not be good when talking about performance, but it works for what I want

HOW TO: Implement a DataSet GROUP BY Helper Class in Visual C# .NET

更多推荐

本文发布于:2023-04-27 23:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表   类型   动态   LINQ   dynamic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!