如何处理在System.Data.DataTableExtensions.CopyToDataTable一个缺陷() [英] How to deal with a flaw in System.Data.DataTableExtensions.CopyToDataTable()

查看:1124
本文介绍了如何处理在System.Data.DataTableExtensions.CopyToDataTable一个缺陷()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,伙计们,让我遇到一些东西,也许是扩展方法的缺陷.CopyToDataTable。

Hey guys, so I've come across something which is perhaps a flaw in the Extension method .CopyToDataTable.

此方法由导入(在VB.NET)System.Data.DataTableExtensions,然后调用对一个IEnumerable的方法。如果你想使用LINQ过滤数据表中,然后还原数据表的末尾,你会做到这一点。

This method is used by Importing (in VB.NET) System.Data.DataTableExtensions and then calling the method against an IEnumerable. You would do this if you want to filter a Datatable using LINQ, and then restore the DataTable at the end.

即:

Imports System.Data.DataRowExtensions
	Imports System.Data.DataTableExtensions

	Public Class SomeClass
    		Private Shared Function GetData() As DataTable
        		Dim Data As DataTable

        		Data = LegacyADO.NETDBCall


        		Data = Data.AsEnumerable.Where(Function(dr) dr.Field(Of Integer)("SomeField") = 5).CopyToDataTable()


        		Return Data

    		End Function
	End Class

在上面的例子中,WHERE过滤可能会返回任何结果。如果发生这种情况CopyToDataTable抛出一个异常,因为没有数据行。

In the example above, the "WHERE" filtering might return no results. If this happens CopyToDataTable throws an exception because there are no DataRows.

为什么?

正确的行为应该是返回一个DataTable与Rows.Count = 0。

The correct behavior should be to return a DataTable with Rows.Count = 0.

谁能想到一个干净的解决方法,这一点,以这样的方式,不管是谁来电CopyToDataTable并不一定意识到这个问题呢?

Can anyone think of a clean workaround to this, in such a way that whoever calls CopyToDataTable doesn't have to be aware of this issue?

System.Data.DataTableExtensions是一个静态类,所以我不能覆盖的行为....任何想法?难道我错过了什么?

System.Data.DataTableExtensions is a Static Class so I can't override the behavior....any ideas? Have I missed something?

欢呼声

更新:

我已经提交了这是一个问题<一href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=422727">Connect.我还是想提出一些建议,但如果你同意我的看法,你可以通过链接投票了这个问题,在连接上述

I have submitted this as an issue to Connect. I would still like some suggestions, but if you agree with me, you could vote up the issue at Connect via the link above

欢呼声

推荐答案

直到微软修复这个问题,这里有一个解决方法:

Until Microsoft fix this issue, here's a work around:

创建了使用CopyToDataTable方法,如果有数据行,如果没有,则返回一个空的DataTable自己的扩展方法。

Create your own Extension method which uses the CopyToDataTable method if there are DataRows, if there aren't, then it returns an empty DataTable.

VB.NET

    Imports System.Data

Namespace CustomExtensions
    Public Module DataRowExtensionsOverride

        <System.Runtime.CompilerServices.Extension()> _
        Public Function CopyToDataTableOverride(Of T As DataRow)(ByVal Source As EnumerableRowCollection(Of T)) As DataTable

            If Source.Count = 0 Then
                Return New DataTable
            Else
                Return DataTableExtensions.CopyToDataTable(Of DataRow)(Source)
            End If

        End Function

    End Module
End Namespace

C#;

public static class DataRowExtensionsOverride
    {

        public static DataTable CopyToDataTableOverride<T>(this IEnumerable<T> Source) where T : DataRow {

            if (Source.Count() == 0) {
                return new DataTable();
            } else {
                return DataTableExtensions.CopyToDataTable<T>(Source);
            }
        }
    }

这篇关于如何处理在System.Data.DataTableExtensions.CopyToDataTable一个缺陷()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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