【问题标题】:Fill Datatable from linq query从 linq 查询填充数据表
【发布时间】:2013-06-09 22:33:01
【问题描述】:

我正在使用下面的代码

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

DataTable dt = query.CopyToDataTable();

但我收到以下错误

不能隐式转换 输入 'System.Collections.Generic.IEnumerable&lt;appointmentcalendar&gt;''System.Collections.Generic.IEnumerable&lt;System.Data.DataRow&gt;'。一个 存在显式转换(您是否缺少演员表?)

【问题讨论】:

  • 该错误告诉您您的约会日历类无法转换为 DataRow。你需要创建一个函数来进行这种转换。
  • system.data.linq.table
  • 你能帮我做同样的事情吗,我是 linq 新手...我认为从数据表中的 linq 查询中获取数据会很容易

标签: c# linq linq-to-dataset


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    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 对象的集合,所以你不能在这里使用它。

    【讨论】:

      【解决方案3】:

      如果有人需要 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
      

      【讨论】:

      • 为了分享您的知识,您可以创建自己的question + answer(在 SO 上完全可以)并将其链接到这个
      【解决方案4】:
      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;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-15
        • 1970-01-01
        • 1970-01-01
        • 2011-02-08
        • 1970-01-01
        相关资源
        最近更新 更多