使用linq将列表结果转换为数据表?

时间:2018-07-09 06:32:13

标签: c# asp.net database linq webforms

使用linq将列表结果转换为数据表吗?

我遇到错误,但是我的数据表结果中有候选ID。

A field or property with the name 'Candidate ID' was not found on the selected data source.

下面是我的代码。我正在尝试将结果数据绑定到gridview,但是错误来了。

    var m_strFilePath = "webserviceurl";

    string xmlStr;
    using (var wc = new WebClient())//using Web client downloading xml string  from WEB API
    {
        xmlStr = wc.DownloadString(m_strFilePath);
    }
    DataSet ds = new DataSet();
    ds = ConvertXMLToDataSet(xmlStr);
    DataTable dt = new DataTable();
    dt = ds.Tables["FL"];



 List<DataTable> result = dt.AsEnumerable().Where(x => x.Field<int>("row_Id") == 0)
     .GroupBy(x => x.Field<int>("row_Id"))
     .Select(grp => grp.CopyToDataTable())
     .ToList();
       GridView1.DataSource = result;
       GridView1.DataBind();

下面是gridview设计代码。

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" GridLines="None" CssClass="table table-bordered table-striped" Width="100%" >
     <Columns>
         <asp:BoundField DataField="Candidate ID" HeaderText="Id" />

         <asp:TemplateField HeaderText="Action">
             <ItemTemplate>
                 <asp:LinkButton ID="lnkbtn_viewdetails" runat="server" OnClick="lnkbtn_viewdetails_Click" CssClass="btn bg-light-blue-active">View Details</asp:LinkButton>
             </ItemTemplate>
         </asp:TemplateField>
     </Columns>
     <EmptyDataTemplate>
         <span style="color: red; font-size: 18px; font-weight: 600; text-align: center; padding-left: 355px;">No records for the entered search details </span>
     </EmptyDataTemplate>
 </asp:GridView>

下面是结果数据表数据。

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您的linq查询将返回DataRow而不是DataTable的列表。 所以我认为应该是List result = ...

然后:

DataTable newDt =新的DataTable();

对于(i = 0; i

然后设置datagridview

相关问题