11

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;
        }
Jango
  • 5,375
  • 14
  • 57
  • 63

7 Answers7

21

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

Rex M
  • 142,167
  • 33
  • 283
  • 313
  • 1
    +1 for the only complete and accurate explanation (while it may be necessary to reference the DataSetExtensions, that won't fix the problem). – Jeff Sternal Oct 20 '09 at 15:25
  • That sounds ok. But please tell me how I gonna get this fixed. If I do this IEnumerable query = //the whole above code, It does not allow me to put Join in Linq Query. – Jango Oct 20 '09 at 15:27
9

It exists in a specific namespace are you importing it?

System.Data.DataTableExtensions.CopyToDataTable() 

Also confirm the addition of this reference

System.Data.DataSetExtensions 
Shankar R10N
  • 4,926
  • 1
  • 21
  • 24
4

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

bruno conde
  • 47,767
  • 15
  • 98
  • 117
2

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

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

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

elder_george
  • 7,849
  • 24
  • 31
0

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.

Arjan Einbu
  • 13,543
  • 2
  • 56
  • 59
  • If that were the problem, then the calls to AsEnumerable would fail, wouldn't they? – Jon Skeet Oct 20 '09 at 15:20
  • I guess you're right. I did only a little bit of research, and no VS to test in... And I see that @Rex M probably has the right answer to this question :-) – Arjan Einbu Oct 20 '09 at 15:26
0

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.

Josh G
  • 644
  • 7
  • 21