2011-03-23 87 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。