2012-10-02 22 views
1

一個下拉列表的結果我有2個表:國家郵政 我retrive在DropDownAddCountry所有的國家和我通過這樣做,顯示wan't在另一個下拉菜單中屬於該國的所有郵件(DropDownAddPostals)。 國家表有一個coulmn CountryID和郵政也有一個coulm CountryID。所以我wan't結果是基於CountryID和CountryID之間的匹配(兩個表):從2個表的數據進行比較,並顯示使用LINQ

我的代碼看起來現在這個樣子(這不是正確的):

using (DB_Entities tt = new DB_Entities()) 
{ 
    var sql = from q1 in tt.Country 
    join q2 in tt.Postal on q1.CountryID equals q2.CountryID 
    select new { q2.Postal1 }; 
    if(sql != null) 
    { 
     DropDownAddPostal= sql.Postal1; 
    } 
} 

乾杯

回答

2

不要使用匿名類型(特別是如果它們不是必需的話)。 您可以使用DataSource-屬性將集合設置爲DropDownList

using (var tt = new DB_Entities()) 
{ 
    var sql = 
     from q1 in tt.Country 
     join q2 in tt.Postal on q1.CountryID equals q2.CountryID 
     select q2.Postal1 

    DropDownAddPostal.DataSource = sql.ToList(); 
    DropDownAddPostal.DataBind(); 
} 
+0

嗨! 這一行在這裏:DropDownAddPostal(sql.Postal1); 附帶了我嘗試使用下拉作爲方法的錯誤,如果我做出這樣也DropDownAddPostal = sql.Posal1 – MishMish

+0

請後從Visual Studio完整的錯誤 –

+0

的dropdownpostal是一個領域,但使用的方法也行不通,這是完整的錯誤信息msg stil – MishMish