0

I have problem when copy query linq to same datatable.

DataTable dt_result = filterkontrak.CopyToDataTable();
            var query = from q in dt_result.AsEnumerable()
                        select new
                        {
                            ChassisNo = q.Field<string>("ChassisNo"),
                            Engineno = q.Field<string>("Engineno"),
                            BuiltYear = q.Field<string>("ManufactureYear"),
                            VehicleType = q.Field<string>("VehicleType"),
                            PlatNo = q.Field<string>("LicensePlate"),
                            Type = q.Field<string>("Type"),
                            ContractNo = q.Field<string>("Brand"),
                            ContractDate = q.Field<string>("ContractNo"),
                            TglGenerate = q.Field<string>("ContractDate"),
                            FileName = q.Field<string>(FileName),
                            Status = q.Field<string>("1"),
                            CreatorID = q.Field<string>(UserId),
                            LastUserId = q.Field<string>(UserId)
                        };

            DataTable dt_result2 = query.AsEnumerable();
            SqlBulkCopy bulkCopy = new SqlBulkCopy(conn.SqlCon());
            bulkCopy.DestinationTableName = "TEMP_VMF_LogAssetMngmt";'

Error:

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

Couldy anybody improve my code ?

jarlh
  • 42,561
  • 8
  • 45
  • 63
Roberts
  • 53
  • 1
  • 7
  • 1
    This is why you'd use an ORM (object relational mapper) like Entity Framework. It handles mapping database columns to object fields, tracks which records have been added, deleted or changed, and writes the changes back to your database on command. – Steve Todd Jul 26 '19 at 09:08
  • Possible duplicate. [that was answered here](https://stackoverflow.com/questions/4460654/best-practice-convert-linq-query-result-to-a-datatable-without-looping) – Power Mouse Jul 26 '19 at 11:50
  • Your error is on `filterkontrak.CopyToDataTable()` but you don't show what `filterkontrak` is, how it is created, or where `q` came from and what that has to do with your overall issue? – NetMage Jul 27 '19 at 00:03

0 Answers0