[英]convert List<ListItem> to datatable in c#
在我的应用程序中,我需要从2个SharePoint列表(ListA和ListB)中获取项目作为listitem-collection,然后如果ListB中的项目匹配(ListA.empid == ListB.empid),则需要从ListA中显示项目。
var iPs = AListItem.AsEnumerable()
.Select(r => r.FieldValues["EmpID"])
.Union(BListItem.AsEnumerable()
.Select(r => r.FieldValues["EmpID"]));
if (iPs.Count() > 0)
{
List<ListItem> sample = (from row in AListItem.AsEnumerable()
join id in iPs
on row.FieldValues["EmpID"] equals id
select row).ToList();
}
但是我需要在数据表中生成结果以便绑定到中继器控件。 如何将List<ListItem>
转换为数据List<ListItem>
?
如我的评论中所述,将sample
用作Repeater
控件的数据源。
该SO链接提供有关如何实现此目的的详细信息:
概述是这样的:
// Here's your object that you'll create a list of
private class Products
{
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public string ProductPrice { get; set; }
}
// Here you pass in the List of Products
private void BindItemsInCart(List<Products> ListOfSelectedProducts)
{
// The the LIST as the DataSource
this.rptItemsInCart.DataSource = ListOfSelectedProducts;
// Then bind the repeater
// The public properties become the columns of your repeater
this.rptItemsInCart.DataBind();
}
ASPX代码:
<asp:Repeater ID="rptItemsInCart" runat="server">
<HeaderTemplate>
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Product Description</th>
<th>Product Price</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ProductName") %></td>
<td><%# Eval("ProductDescription")%></td>
<td><%# Eval("ProductPrice")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
我同意Ric:也许最好绑定List。
但是,如果您确实需要转换为数据表,请检查以下链接:
http://www.c-sharpcorner.com/UploadFile/1a81c5/list-to-datatable-converter-using-C-Sharp/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.