문제

In the following link: http://msdn.microsoft.com/en-us/library/bb386921.aspx

"Creating a Custom CopyToDataTable Method" Paragraph > Example

I tried to do something similar. This is my code:

ObjDA = new OleDbDataAdapter(querySQL, conection);

//Create a DataSet object:
ObjDS = new DataSet();

ObjDA.Fill(ObjDS, "Table1");

DataTable MyTable = ObjDS.Tables["Table1"];

//IEnumerable<DataRow> query =
var query =
    from user in MyTable.AsEnumerable()
    where user.Field<string>("Name").StartsWith("c", true, null)
    select new
    {
        Name = user.Field<string>("Name")
    };

DataTable orderTable = query.CopyToDataTable();

The issue is in "query.CopyToDataTable": There isn't an implicit reference conversion "AnonymoustType#1" to "System.Data.DataRow". If I write "select user", it works ok but the problem is: select new.

If I write:

query = (...) as IEnumerable<DataRow>

query always return null.

I don't know what to do.

도움이 되었습니까?

해결책

Did you actually follow the procedure that is described in the article? As the article explains, CopyToDataTable by default only works on an IEnumerable<DataRow>. If you want to use the method on a custom type you need to Implement CopyToDataTable Where the Generic Type T Is Not a DataRow

All of the examples in that section assume you have followed the process and created the custom CopyToDataTable<T> method first.

다른 팁

Why query in your code example should have CopyToDataTable function? query is an IEnumerable where T happens to be anonymous type with one property (Name):

select new
                {
                    Name = user.Field<string>("Name")
                };

If you really want to enjoy the CopyToDataTable method, you should stick to the original code example:

DataTable orders = ds.Tables["SalesOrderHeader"];

IEnumerable<DataRow> query =
    from order in orders.AsEnumerable()
    where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1)
    select order;

DataTable boundTable = query.CopyToDataTable<DataRow>();

You should create a custom CopyToDataTable method in order to support what you want: http://msdn.microsoft.com/en-us/library/bb669096.aspx

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top