0

My goal is that join operation are being done and that query value have to store in Datatable.

var query = from db_data1 in DBData1.AsEnumerable()
            join cust_data1 in DBData2.AsEnumerable()
            on db_data1.Field<string>("FULLNAME").Trim() equals
            cust_data1.Field<string>("Name").Trim()
            where 
            cust_data1.Field<string>("Apartment").Trim() == db_data1.Field<string>("SERVICE_APARTMENT").Trim()
            &&   cust_data1.Field<string>("Name").Trim() == db_data1.Field<string>("FULLNAME").Trim()
            select new
              {
                  Name = db_data1.Field<string>("FULLNAME"),
                  ACCOUNT_NUMBER = db_data1.Field<string>("ACCOUNT_NUMBER")
              };

           DataTable merged;
           if (query.Any())
               merged = query.**CopyToDataTable**();
           else
               merged = DBData1.Clone();

But here i am getting error in CopyToDataTable() fuction that

The type 'AnonymousType#1' cannot be used as type parameter 'T' in the generic type or method 'System.Data.DataTableExtensions.CopyToDataTable(System.Collections.Generic.IEnumerable)'. There is no implicit reference conversion from 'AnonymousType#1' to 'System.Data.DataRow'.

How to resolve this issue?

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
Rajesh D
  • 167
  • 1
  • 13

1 Answers1

0

The query is returning an IEnumerable of anonymous type because you're creating a type-less sequence with select new { }

If you select using a definitive destination type - ie, select CustDataRow - you'll be able to use CopyToDataTable(), though for your purposes this might require you to create a helper object.

Nick S.
  • 78
  • 4
  • 1
    Just realized this is indeed explained well [here](https://stackoverflow.com/questions/1253725/convert-ienumerable-to-datatable). Thanks Rahul ^^^ – Nick S. Nov 24 '15 at 12:14