2011-09-05 68 views
0

我有這樣綁定LINQ查詢結果直放站

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作爲商務類的成員。我有一個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?

+0

您可以添加BusinessDetails類的定義嗎?它是否有商業物業? – kmcc049

+0

@ kmcc049新增。 –

+0

刪除'DataKeyField =「UserId」'然後嘗試 – Ankur

回答

1

我將其更改爲

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

假設該業務類有一個BusinessName財產?

+0

我試過,但它沒有工作。 –

+0

確定第一停靠港是否有商業物業?其次是列表非空? – kmcc049

+0

如果我在查詢後有一個for循環,我可以看到所有的商業名稱與Response.Write(detail.BusinessData.BusinessName +「
」); –

1

我想你在ItemTemplate標籤想

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

,而不是

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

我不認爲你在BusinessDetails類有一個UserId字段。 您應該用唯一的鍵列名稱替換它或將其刪除。我看到你可以訪問BussinessName,所以訪問說明符是公開的。

該綁定是正確的。如果dataList控件dlBusiness找不到該屬性,則拋出異常。 也許問題的原因來自businessList返回一個空的BussinessName。