2011-03-23 64 views
3

我是LINQ的新手。我有一個使用LINQ填充的GridView。我的LINQ語句正在從上一頁獲取查詢字符串。查詢字符串是字符串格式。以下是代碼:加入不在LINQ聲明中工作

protected void Page_Load(object sender, EventArgs e) 
    { 
     string getEntity = Request.QueryString["EntityID"]; 
     int getIntEntity = Int32.Parse(getEntity); 
     OISLinqtoSQLDataContext db = new OISLinqtoSQLDataContext(); 
     var tr = from r in db.Users 
       join s in db.Entities on r.UserID equals s.ID 
       where s.ID = Request.QueryString["EntityID"] 
       select new 
       { 
        //To Show Items in GridView! 
       }; 

     GridView1.DataSource = tr; 
     GridView1.DataBind(); 

    } 

s.ID不等於QueryString。 S.ID是一種int類型,QS是一種字符串。我應該如何將此QS轉換爲整數?謝謝!

+0

您已經將QueryString轉換爲int - 'getIntEntity'。只需在你的查詢中使用它。 – kevingessner 2011-03-23 15:46:35

+0

是的,那是凱文。但是在我的JOIN中有一個錯誤顯示:錯誤\t連接子句中的一個表達式的類型不正確。在「加入」的調用中,類型推斷失敗。 – 2011-03-23 16:00:18

回答

3

確保您使用的==不等於,並使用「getEntity」變量的INT版本。

protected void Page_Load(object sender, EventArgs e) 
{ 
    string getEntity = Request.QueryString["EntityID"]; 
    int getIntEntity = Int32.Parse(getEntity); 
    OISLinqtoSQLDataContext db = new OISLinqtoSQLDataContext(); 
    var tr = from r in db.Users 
      join s in db.Entities on r.UserID equals s.ID 
      where s.ID == getIntEntity 
      select new 
      { 
       //To Show Items in GridView! 
      }; 

    GridView1.DataSource = tr; 
    GridView1.DataBind(); 

} 
2

你應該用你解析整數值,而不是:

var tr = from r in db.Users 
      join s in db.Entities on r.UserID equals s.ID 
      where s.ID == getIntEntity 
      select new 
      { 
       //To Show Items in GridView! 
      }; 
+0

是的,做了裏德。但是在我的JOIN中有一個錯誤顯示:錯誤\t連接子句中的一個表達式的類型不正確。在「加入」的調用中,類型推斷失敗。 – 2011-03-23 16:01:51

+0

@ klm9971:重新設置db.Entities.ID和db.Users.UserID的類型? – 2011-03-23 16:04:28

0

好吧!這裏是:

我在實體表中的ID是PK,用戶表中的EntityID是一種int類型,但沒有約束。 所以這是

1-MANY Relationship。