2

我正在使用下面的代码

IEnumerable<DataRow> query = from c in at.appointmentcalendars.AsEnumerable() 
                             select c;

DataTable dt = query.CopyToDataTable();

但我收到以下错误

无法将类型隐式转换'System.Collections.Generic.IEnumerable<appointmentcalendar>''System.Collections.Generic.IEnumerable<System.Data.DataRow>'. 存在显式转换(您是否缺少演员表?)

4

4 回答 4

9

由于查询返回 DataRow 类型的 IEnumerable,因此您必须指定要插入数据表的内容,在本例中为 DataRow。

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

如果你用过

var query = //linq query of what you need for new datatable....
DataTable dt = query.CopyToDataTable();

你的表名是 dt 所以从原始 dt 中选择你需要的

var query = from c in db.something
            where c.othersomething == "onlyyouknow"
            orderby c.othersomething
            select new { NewObject = c.othersomething };

DataTable MyDataTable = new DataTable();
myDataTable.Columns.Add(
    new DataColumn()
    {
        DataType = System.Type.GetType("System.String"),//or other type
        ColumnName = "Name"      //or other column name
    }
);

foreach (var element in query)
{
    var row = MyDataTable.NewRow();
    row["Name"] = element.NewObject;
    myDataTable.Rows.Add(row);
}
于 2013-06-13T13:56:02.750 回答
0

at.appointmentcalendars不是一个DataTable。因此,您的查询返回appointmentcalendar对象集合,这些对象不能转换为DataRow.

您需要使用CopyToDataTableMSDN 文章How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow 中的方法:

IEnumerable<appointmentcalendar> query = at.appointmentcalendars.AsEnumerable();
DataTable dt = query.CopyToDataTable();

默认CopyToDataTable()方法仅适用于DataRow对象集合,因此您不能在此处使用它。

于 2013-06-13T13:54:02.663 回答
0
DataTable getListRow(DataTable dt, int intSndBCode, int intPostManSCode)
    {
        IEnumerable<DataRow> results = (from MyRows in dt.AsEnumerable()
                       where
                       MyRows.Field<int>("m_sndBCode") == intSndBCode
                       &&
                       MyRows.Field<int>("m_postManSCode") == intPostManSCode
                       select MyRows);
        DataTable dtNew = results.CopyToDataTable();
        return dtNew;
    }
于 2017-06-13T18:05:54.627 回答
0

如果有人需要 VB.net 和聚合函数中的解决方案:

    Dim MyDataTable As DataTable = New DataTable
    MyDataTable.Columns.Add("ProviderName", GetType(String))
    MyDataTable.Columns.Add("TotalNumSale", GetType(Integer))
    MyDataTable.Columns.Add("TotalValSale", GetType(Double))

    Dim testquery = (From p In adviceProductData _
                                         Where p.IsOffPanel = False _
                                   Group By p.ProviderName _
                                   Into _
                                   totalNoOfSale = Sum(p.NoOfSales), _
                                   totalValueOfSale = Sum(p.ValueOfSales)
                                   Select New With {
                                       .ProvName = ProviderName,
                                        .TotSale = totalNoOfSale,
                                       .TotVal = totalValueOfSale
                                       }).ToList()

    Dim item
    For Each item In testquery
        Dim row = MyDataTable.NewRow()
        row("ProviderName") = item.ProvName
        row("TotalNumSale") = item.TotSale
        row("TotalValSale") = item.TotVal
        MyDataTable.Rows.Add(row)
    Next
于 2016-09-02T06:24:25.700 回答