Linq到数据表问题 [英] Linq to Datatable Issues

查看:52
本文介绍了Linq到数据表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

真的很抱歉提出这个问题,因为一旦您知道它就相当简单.

我试图使用linq加入数据表并从两个表中返回列,然后将结果插入到另一个数据表中.

问题是我不能使用query.CopyToDataTable()方法,因为查询(或其他内容)中存在匿名类型.请参见下面的代码.

我一直在网上寻找绝对的年龄!我已经在MSDN上看到了ObjectShredder类的建议,但不能完全解决,我尝试了许多其他可能的解决方案,可惜没有结果.

有人可以指出我出了问题的地方,还是可以快速找到如何使ObjectShredder类解决此问题的指针?

在这个阶段的任何帮助都非常欢迎!

在此先感谢

克里斯


原始代码:

Hi all,

Really sorry to ask this as im sure its fairly simple once you know.

Im trying to join to datatables using linq and return columns from both tables and then insert the result into another datatable.

The problem is that I cant use the query.CopyToDataTable() method as there are anonymous types in the query (or something). Please see the code below.

I have been looking online now for absolutely ages! I have seen the ObjectShredder class advice given on MSDN but cant quite work it out, and i have tried a huge variety of other potential solutions, alas to no avail.

Could someone please either point out where im going wrong or may be a quick pointer on how to get the ObjectShredder Class to resolve this?

Any help at this stage is more than welcome!

Thanks in advance

Chris


The original bit of code:

Dim query = From a In dt _
    Join b In dtLossEvents _
    On _
    a.Field(Of Integer)("EventID") Equals b.Field(Of Integer)("EventID") _
    Select New With { _
                    a, _
                    b _
                    }








这是最新的有缺陷的化身:








This it the latest flawed incarnation:

Dim query = From a In dt _
            Join b In dtLossEvents _
            On _
            a.Field(Of Integer)("EventID") Equals b.Field(Of Integer)("EventID") _
            Select New With { _
                             .Gross_Loss = a.Field(Of Double ("Gross_Loss"), _
                             .Net_Loss = a.Field(Of Double)("Net_Loss"), _
                             .EventID = b.Field(Of Integer)("EventID") _
}

推荐答案

Hi Chris;<br>
First add the the class and module to your project:
    Public Class ObjectShredder(Of T)
    Public Module CustomLINQtoDataSetMethods

They are located on the web page
    How to: Implement CopyToDataTable<T> Where the Generic Type T Is Not a DataRow
    <a href="http://msdn.microsoft.com/en-us/library/bb669096.aspx">http://msdn.microsoft.com/en-us/library/bb669096.aspx</a>

Have your two DataTables filled

Then modify your query to add the method AsEnumerable() to both tables as shown below

<pre>Dim query = From a In dt.AsEnumerable() _
            Join b In dtLossEvents.AsEnumerable() _
            On _
            a.Field(Of Integer)("EventID") Equals b.Field(Of Integer)("EventID") _
            Select New With { _
                .Gross_Loss = a.Field(Of Double ("Gross_Loss"), _
                .Net_Loss = a.Field(Of Double)("Net_Loss"), _
                .EventID = b.Field(Of Integer)("EventID") _
            }


然后在查询中执行方法CopyToDataTable
昏暗表As DataTable = query.CopyToDataTable()

Fernando



Then execute the method CopyToDataTable on your query
Dim table As DataTable = query.CopyToDataTable()

Fernando



这篇关于Linq到数据表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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