【问题标题】:LINQ to SQL error message: 'Where' not foundLINQ to SQL 错误消息:找不到“哪里”
【发布时间】:2008-10-15 13:05:33
【问题描述】:

我正在尝试开始使用 LINQ,特别是 LINQ to SQL,但我遇到了一些困难

我已经用 SqlMetal 尝试过这个,现在在 Visual Studio 中使用数据库表设计器,我不断收到类似的错误,就像在这段代码中,使用我在 VS2008 中使用数据库布局设计器创建的数据上下文。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        string usn = UserNameBox.Text;
        string pss = PassBox.Text;

        if (usn == "" || pss == "")
            return;

        DataClassesDataContext dc = new DataClassesDataContext();
        var user = from u in User
                   where u.UserName == usn
                   select u;

    }
}
}

我收到一条关于 where 的错误消息:找不到源类型“System.Security.Principal.IPrincipal”的查询模式的实现。还有:找不到“哪里”。

当我尝试使用 SqlMetal 的结果时,我遇到了类似的情况。我删除了该来源并重新开始使用设计器。我一定在这里遗漏了一些东西,但我不知道是什么。既然我使用的是 LINQ to SQL,这些表不应该实现我需要的东西,还是我需要做一些额外的事情来实现它?

【问题讨论】:

    标签: c# linq linq-to-sql


    【解决方案1】:

    尝试将User 更改为dc.User

    var user = from u in dc.User
                       where u.UserName == usn
                       select u;
    

    User 是属性System.Web.UI.Page.User

    【讨论】:

      【解决方案2】:

      试试

      var user = from u in dc.User
                         where u.UserName == usn
                         select u;
      

      【讨论】:

      • 那行得通。出于某种原因,我没有考虑在其中引用 dc。谢谢。
      【解决方案3】:

      您不应该使用您在查询中某处创建的 dc 吗?

      类似:

      protected void Page_Load(object sender, EventArgs e) 
      { 
        if (Page.IsPostBack) 
        {
          string usn = UserNameBox.Text; 
          string pss = PassBox.Text; 
          if (usn == "" || pss == "")            
            return; 
          DataClassesDataContext dc = new DataClassesDataContext(); 
          var user = from u in dc.User where u.UserName == usn select u; 
        } 
      }
      

      【讨论】:

      • 好点。嗯,我尝试按照我在网上找到的示例进行操作,但我一定错过了将数据上下文放在 var 中的位置。
      【解决方案4】:

      您的查询表达式应该使用 dc.User,而不仅仅是 User:

      DataClassesDataContext dc = new DataClassesDataContext();
      var user = from u in dc.User
                 where u.UserName == usn
                 select u;
      

      如果您启用了复数,则可能是 dc.Users。

      【讨论】:

        【解决方案5】:

        您所指的用户(当您不使用 dc.User 时)是 Page.User 成员。这就是智能感知不抱怨的原因。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多