简体   繁体   English

Linq分组查询到DataTable

[英]Linq grouping query to DataTable

Trying to return a Linq group by query into a DataTable. 尝试通过查询将Linq组返回到DataTable。 Getting the error 得到错误

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<System.Data.DataRow>'. 无法将类型'System.Collections.Generic.IEnumerable <AnonymousType#1>'隐式转换为'System.Collections.Generic.IEnumerable <System.Data.DataRow>'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)

The I am querying a DataTable named Vendors where the data would be as follows: 我正在查询一个名为Vendors的数据表,其中的数据如下:

Vendor         Name 
654797         Lowes
897913         Home Depot
800654         Waffle House

The Vendor is stored as char(6) in the DB and name as char as well... don't ask me why, I just work here :) 供应商在数据库中存储为char(6),名称也存储为char ...不要问我为什么,我只是在这里工作:)

    DataTable VendorsDT = New DataTable();
    DataColumn VenName = VendorsDT.Columns.Add("Name", typeof (string));
    DataColumn VenCode = VendorsDT.Columns.Add("Vendor", typeof(string));


    IEnumerable<DataRow> Vendors = from row in alertsDT.AsEnumerable()
                                   group row by new { Name = row.Field<string>("Name"), Vendor = row.Field<string>("Vendor") } into z
                                   select new
                                   {
                                        Name = z.Key.Name,
                                        Vendor = z.Key.Vendor,                        

                                   };



    VendorsDT = Vendors.CopyToDataTable();

This is not going to work, as you are projecting your original query of alertsDT into an anonymous type, which would not be able to be referenced by your IEnumerable<DataRow> Vendor variable, because your query is not a sequence of DataRows. 这将无法正常工作,因为您将alertsDT的原始查询投影为匿名类型, IEnumerable<DataRow> Vendor变量将无法引用该匿名类型,因为查询不是 DataRows序列。

Given that you are performing a grouping, and that you have also already set up your VendorsDT table with the desired columns, the path of least resistance is to fix your Vendor variable type (use var for type inference) and then loop over the result to populate your second table. 假设您正在执行分组,并且还已经用所需的列设置了VendorsDT表,那么阻力最小的路径就是修复您的Vendor变量类型(使用var进行类型推断),然后将结果循环到填充第二个表。

var Vendors = /* your unchanged query omitted */ 

foreach (var item in Vendors) 
{
    VendorsDT.Rows.Add(item.Name, item.Vendor);
}

As a note, I've used your variable names, although it is convention in C# to use lower case letters to start local variable names, with upper case typically left for method names, properties, etc. So you would favor vendors over Vendors and vendorsDT (or vendorsTable ) over VendorsDT , for example. 作为一个说明,我用你的变量名,虽然这是惯例在C#中使用小写字母开始局部变量名,以大写字母通常留给方法名,属性等,所以你将有利于vendorsVendorsvendorsDT (或vendorsTable )超过VendorsDT ,例如。

This is a more linq way of doing 这是一种更方便的方法

  var query = from row in alertsDT.AsEnumerable()
                      group row by new { Name = row.Field<string>("Name"), Vendor = row.Field<string>("Vendor") } into z
                      select new 
                      {
                        Name=  z.Key.Name,
                        Vendor=  z.Key.Vendor,

                      };
        VendorsDT = query.CopyToDataTable();

Here define the extension method 'CopyToDataTable()' as specified in the following MSDN article. 在此定义扩展方法'CopyToDataTable()',如以下MSDN文章中所指定。

The CopyToDataTable method takes the results of a query and copies the data into a DataTable, which can then be used for data binding. CopyToDataTable方法获取查询结果并将数据复制到DataTable中,然后可将其用于数据绑定。 The CopyToDataTable methods, however, only operate on an IEnumerable source where the generic parameter T is of type DataRow. 但是,CopyToDataTable方法仅在通用参数T为DataRow类型的IEnumerable源上运行。 Although this is useful, it does not allow tables to be created from a sequence of scalar types, from queries that project anonymous types, or from queries that perform table joins. 尽管这很有用,但是它不允许从标量类型序列,投影匿名类型的查询或执行表联接的查询创建表。

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

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