从linq查询中填充数据表

时间:2013-06-13 13:49:34

标签: c# linq linq-to-dataset

我正在使用以下代码

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 个答案:

答案 0 :(得分:8)

由于查询返回类型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);
}

答案 1 :(得分:0)

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

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

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

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

答案 2 :(得分: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

答案 3 :(得分: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;
    }