使用联接到数据表中加载Linq-to-DataSet查询的结果 [英] Load result of Linq-to-DataSet query with join into datatable

查看:104
本文介绍了使用联接到数据表中加载Linq-to-DataSet查询的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个linq到数据集查询,该查询连接了两个表并从每个表中提取所需的参数.我需要将它们放入DataTable中以绑定到DataGridView.我发现在MSDN上执行此操作的示例是一个简单的示例,该示例从单个表中获取单个值,但是当我尝试更改查询以使其遵循时,我无法执行此操作. CopyToDataTable()方法要求将查询分配给IEnumerable< datarow> ;,但是当我这样做时,我被告知需要显式强制转换;但是强制转换在运行时失败,但以下情况除外:

无法强制转换类型为``< JoinIterator> d__61`4 [System.Data.DataRow,System.Data.DataRow,System.Int32,< f__AnonymousType0`1 [System. Int32]]''键入"System.Collections.Generic.IEnumerable`1 [System.Data.DataRow]".

原始工作查询:

I have a linq to dataset query that joins two tables and extracts the desired parameters from each. I need to get them into a DataTable to bind to a DataGridView. The example I found for doing this on MSDN is a trivial example taking a single value from a single table, but when I tried to change my query to follow it I was unable to do so. The CopyToDataTable() method requires that the query be assigned to a IEnumerable<datarow>, but when I do so I''m told that an explict cast is needed; but the cast fails at runtime with the exception:

Unable to cast object of type ''<JoinIterator>d__61`4[System.Data.DataRow,System.Data.DataRow,System.Int32,<>f__AnonymousType0`1[System.Int32]]'' to type ''System.Collections.Generic.IEnumerable`1[System.Data.DataRow]''.

Original working query:

var query = MyDataSet.Table1.AsEnumerable().Join(MyDataSet.Table2.AsEnumerable(),
        table1 => table1.Field<Int32>("Table1_Id"),
        table2 => table2.Field<Int32>("Table1_Id"),
        (table1, table2) => new
        {
                Table1ID = table1.Field<Int32>("Table1_Id")
                //Other parameters commented out to simplify the example
        });



使用显式强制转换的非工作查询:



Non-working query with explicit cast:

IEnumerable<DataRow> query = (IEnumerable<DataRow>)MyDataSet.Table1.AsEnumerable().Join(MyDataSet.Table2.AsEnumerable(),
        table1 => table1.Field<Int32>("Table1_Id"),
        table2 => table2.Field<Int32>("Table1_Id"),
        (table1, table2) => new
        {
                Table1ID = table1.Field<Int32>("Table1_Id")
                //Other parameters commented out to simplify the example
        });

推荐答案

也许是
Perhaps the answer to a similar question will be of use to you.

"when I do so I''m told that an explict cast is needed" -- what''s the error message you get?


这篇关于使用联接到数据表中加载Linq-to-DataSet查询的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆