【问题标题】:Join is not working in LINQ statement加入在 LINQ 语句中不起作用
【发布时间】:2011-03-23 15:44:17
【问题描述】:

我是 LINQ 的新手。我有一个使用 LINQ 填充的 GridView。我的 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 是 string 类型。我应该如何将此 QS 转换为整数?谢谢!

【问题讨论】:

  • 您已经将 QueryString 转换为 int - getIntEntity。只需在查询中使用它即可。
  • 是的,凯文做到了。但是在我的 JOIN 中有一个错误显示:错误连接子句中的表达式之一的类型不正确。调用“加入”时类型推断失败。

标签: c# asp.net linq-to-sql web-applications


【解决方案1】:

确保您使用的是 == 而不是 = 并使用“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!
                 };
    

    【讨论】:

    • 是的,Reed 做到了。但是在我的 JOIN 中有一个错误显示:错误连接子句中的表达式之一的类型不正确。调用“加入”时类型推断失败。
    • @klm9971:db.Entities.ID 和 db.Users.UserID 的类型是什么?
    【解决方案3】:

    好的!这里是:

    我在 Entity 表中的 ID 是 PK,而在 User Table 中的 EntityID 是 int 类型但没有约束。 所以这是

    1-MANY 关系。

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2012-08-05
      • 2015-08-17
      • 2011-07-28
      相关资源
      最近更新 更多