我从三个对象列表中返回一个这样的列表 * 感谢@sehe
`var joined = from p in personList
join par in relations
on p.Id equals par.PersonId
join a in addressList
on a.Id equals par.AddressId
select new { Person = p, Address = a };`
如何将 joined 设置为 ListView 的数据源并访问 aspx 页面中的属性?
好吧,这里还有一些代码可能会有所帮助,因为我在这方面得到了两个不同的答案。
//后面的代码
protected void Page_Init(object sender, EventArgs e)
{
List<Customer> customers = Customer.GetAllCustomers();
List<Address> addresses = Address.GetAllAddresses();
List<AddressRelation> addressRelations = AddressRelation.GetAllAddressRelations();
var results = from c in customers
join rel in addressRelations
on c.CustomerID equals rel.CustomerID
join a in addresses
on rel.CustomerAddressID equals a.CustomerAddressID
select new
{
FirstName = c.FirstName,
LastName = c.LastName,
PhoneNumber = c.PhoneNumber,
AddressLine = a.AddressLine1,
CustomerCity = a.City,
CustomerZip = a.ZipCode
};
ListView1.DataSource = results;
ListView1.DataBind();
这是我的 ListView :
`<asp:ListView ID="ListView1" runat="server" >`
`<LayoutTemplate>`
<ul style="float:left; width:250px">
<asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li><%# Eval("FirstName") %></li>
<li><%# Eval("AddressLine") %></li>
</ItemTemplate>
<ItemSeparatorTemplate><hr /></ItemSeparatorTemplate>
</asp:ListView>
最佳答案
您只需设置 ListView.DataSource = joined
, 然后调用 DataBind()
您可以通过 Eval()
在您的 ListView 模板中访问它们或 Bind()
查看MSDN documentation for the ListView control更多例子
关于c# - 如何在 aspx 页面中访问 linq 查询的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7167575/