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

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).

public static bool SetPhysicianAsNotonServer(DataTable dt)
        {
            DataTable dtPhysicianServer = dt;
            DataTable dtPhysicianClient = GetPhysicianClient();

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

            DataTable FilterDt = query; //query.CopyToDataTable();
            //YET TO DO CODE HERE
            return true;
        }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your select statement is returning a sequence of strings (IEnumerable<string> or IQueryable<string>), not a sequence of DataRows. CopyToDataTable() is only available on IEnumerable<T> where T is or derives from DataRow.

Instead of select new { ... } – which will just get you a new sequence of that type, try:

select CPhysician;

Which should return the desired sequence of CPhysician rows.

Edit
If you wish to convert a non-datatable-derived T to a datatable, MSDN has a sample class that reflects out any type and performs the conversion.

http://msdn.microsoft.com/en-us/library/bb669096.aspx

Method 2

It exists in a specific namespace are you importing it?

System.Data.DataTableExtensions.CopyToDataTable()

Also confirm the addition of this reference

System.Data.DataSetExtensions

Method 3

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

Method 4

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

Method 5

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

Method 6

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.

Method 7

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x