编程语言
首页 > 编程语言> > CodeGo.net>如何访问ASPX页面LINQ查询的属性?

CodeGo.net>如何访问ASPX页面LINQ查询的属性?

作者:互联网

我从三个对象列表中返回了这样的列表*感谢@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 };`

如何设置join作为列表视图的数据源并访问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();

这是我的列表视图:

            `<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>

解决方法:

这里有两个问题:

您尚未完全枚举该集合-您可能应该调用诸如.ToList()或.ToArray()之类的聚合器.

此外,您还将创建一个匿名对象作为结果.在该代码的特定范围之外,没有任何对象知道对象包含哪些属性.如果要在其他地方使用该对象,则应切换到具有已知属性的预定义类.

标签:linq-to-objects,linq,asp-net,c
来源: https://codeday.me/bug/20191102/1990205.html