I’m returning a list like this from three lists of objects * thanks to @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 };`
How do I set joined as a datasource for a listview and access the properties in the aspx page?
Okay here’s some more code maybe that will help since I’m getting two different answers on this.
// the code behind
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();
Here’s my 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>
Two problems here:
You haven’t fully enumerated the set yet – you should probably call an aggregator like
.ToList()or.ToArray().In addition, you’re creating an anonymous object as your result. Outside the specific scope of that code, nothing has any idea what properties that object contains. You should switch to a pre defined class that has known properties if you want to use that object elsewhere.