Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

linq

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;
        }
like image 758
Jango Avatar asked Oct 20 '09 15:10

Jango


2 Answers

It exists in a specific namespace are you importing it?

System.Data.DataTableExtensions.CopyToDataTable() 

Also confirm the addition of this reference

System.Data.DataSetExtensions 
like image 144
Shankar R10N Avatar answered Oct 21 '22 13:10

Shankar R10N


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

like image 41
bruno conde Avatar answered Oct 21 '22 14:10

bruno conde