tags:

views:

380

answers:

6

Hi

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;
        }
A: 

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 Ramachandran
DataTable.AsEnumerable would also fail if this was not included.
Rex M
+1  A: 

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

Darin Dimitrov
It is not a reference issue. DataSetExtensions assembly is already there.
Novice
+7  A: 

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.

Rex M
@anonymous why the DV?
Rex M
+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
That sounds ok. But please tell me how I gonna get this fixed. If I do this IEnumerable<DataRow> query = //the whole above code, It does not allow me to put Join in Linq Query.
Novice
+1 agree with Jeff
Christian Hayter
`select CPhysician` should do the trick.
Christian Hayter
Christian Hayter. Thanks, your trick worked.
Novice
A: 

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

elder_george
A: 

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
If that were the problem, then the calls to AsEnumerable would fail, wouldn't they?
Jon Skeet
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
A: 

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
no didn't work. Same thing...I cant see .CopyToDataTable()
Novice
Have you tried referencing DataSetExtensions as others have mentioned?
bruno conde