Why am I not getting .CopyToDataTable() in Linq Query()

前端 未结 7 1047
既然无缘
既然无缘 2020-12-11 02:22

This following code example is borrowed from MSDN here. I am not getting query.CopyToDataTable() available in my code. (see the commented line in my following code).

相关标签:
7条回答
  • 2020-12-11 03:10

    You need to reference the System.Data.DataSetExtensions assembly and use the System.Data namespace.

    0 讨论(0)
  • 2020-12-11 03:11

    This issue for me was caused by using Dotnet Core 2.0, which appears to have no way to turn an IEnumerable back into a DataTable after it's converted.

    Just adding this for anyone that comes across this question with the same issue.

    0 讨论(0)
  • 2020-12-11 03:12

    Navigating further into MSDN online brings me to this page: http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.aspx

    It says its in the System.Data namespace (using System.Data) and you need to reference the System.Data.DataSetExtensions.dll.

    0 讨论(0)
  • 2020-12-11 03:14

    It exists in a specific namespace are you importing it?

    System.Data.DataTableExtensions.CopyToDataTable() 
    

    Also confirm the addition of this reference

    System.Data.DataSetExtensions 
    
    0 讨论(0)
  • 2020-12-11 03:14

    I think that's because your creating a anonymous type to hold the Field object. Try this:

        var query = from SPhysician in dtPhysicianServer.AsEnumerable()
                    join CPhysician in dtPhysicianClient.AsEnumerable()
                    on SPhysician.Field<string>("PhysicianNumber") equals
                        CPhysician.Field<string>("PhysicianNumber")
                    select CPhysician;
    
        DataTable FilterDt = query.CopyToDataTable();
    

    Definition of CopyToDataTable<T>:

    public static DataTable CopyToDataTable<T>(
        this IEnumerable<T> source
    )
    where T : DataRow
    

    So what you select with the query must be of type IEnumerable<T> where T extends DataRow

    0 讨论(0)
  • 2020-12-11 03:21

    Have you referenced System.Data.DataSetExtensions assembly? This extension method is defined there.

    0 讨论(0)
提交回复
热议问题