将LINQ查询结果绑定到Repeater

时间:2011-09-05 03:17:33

标签: c# asp.net linq datalist

我有这样的查询

 var businessList = (from u in _DB.Users
join cat in _DB.BusinessCategories on u.UserId equals cat.UserId
select new BusinessDetails(u.Businesses.FirstOrDefault(), u)).Skip(0).Take(10);

dlBusiness.DataSource = businessList.ToList();
dlBusiness.DataBind();

我将Businessname作为Business类的成员。我有一个DataList,如下所示

 <asp:DataList ID="dlBusiness" DataKeyField="UserId" runat="server" >
    <ItemTemplate>
            <%#Eval("Business.BusinessName")%>
    </ItemTemplate>
 </asp:DataList>

这是课程定义

public class BusinessDetails
    {

        public BusinessDetails(Business business,User user){
            this._Business = business;
            this._UserDetails = user;

        }

        private string _distance;
        public string Distance
        {
            get { return _distance; }
            set { _distance = value; }
        }

        private Business _Business;
        public Business BusinessData
        {
            get { return _Business; }
            set { _Business = value; }
        }
        private User _UserDetails;

        public User UserDetails
        {
            get { return _UserDetails; }
            set { _UserDetails = value; }
        }


    }

但这显示空白?如何将其绑定到DataList?

2 个答案:

答案 0 :(得分:1)

我会把它改成

DataBinder.Eval(Container.DataItem, "BusinessData.BusinessName")

假设商务舱有BusinessName属性?

答案 1 :(得分:1)

我想你想要

<%#Eval("BusinessData.BusinessName")%>

而不是

<%#Eval("Business.BusinessName")%>

ItemTemplate标记中的

我认为BusinessDetails课上没有UserId字段。 您应该用唯一的键列名替换它或删除它。我看到你可以访问BussinessName所以访问说明符是公共的。

绑定是正确的。如果dataList控件dlBusiness找不到该属性,则抛出异常。 也许问题的原因来自businessList返回空BussinessName的事实。