c# - 如何在 aspx 页面中访问 linq 查询的属性?

标签 c# asp.net linq linq-to-objects

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

相关文章:

c# - Linq 表达式中的条件

c# - 如何列出实体集的属性?

ASP.NET 后台处理

c# - 链接按钮有一些问题

c# - 为 Linq 中的分组项目添加编号

c# - 没有从 ApplicationDbContext 到 Microsoft.EntityFrameworkCore.DbContext 的隐式引用转换

c# - 附加信息 : Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'

c# - MongoDB Linq 有 "Explain Query"吗?

c# linq 将三个列表连接到嵌套字典

C#Excel : Correct way to get Rows and Columns count