Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么LINQ to实体在这个select语句中抛出错误?_Linq_Select - Fatal编程技术网

为什么LINQ to实体在这个select语句中抛出错误?

为什么LINQ to实体在这个select语句中抛出错误?,linq,select,Linq,Select,我有两个完全相同的选择,但其中一个(getBranchsForUser)抛出错误 public User GetUserByUsername(string username) { var db = new AdventureWorksEntities(); User user = (from u in db.tabUser where u.strUsername.Equals(username, StringComparison.Curren

我有两个完全相同的选择,但其中一个(
getBranchsForUser
)抛出错误

public User GetUserByUsername(string username) 
{
    var db = new AdventureWorksEntities();
    User user = (from u in db.tabUser 
                 where u.strUsername.Equals(username, StringComparison.CurrentCultureIgnoreCase)
                 select new User 
                            { 
                               strUsername = u.strUsername,  
                               intID = u.intUserID,   
                            }
                ).FirstOrDefault();
    user.branchsList = getBranchsForUser(user.intID);
    return user;
}

public List<Branch> getBranchsForUser(int userID)
{
  var db = new AdventureWorksEntities();
  List<Branch> branchsList = (from u in db.tabBranchs
                              where u.intUserID.Equals(userID)
                              select new Branch
                                         {
                                            dblGoal = u.dblGoal,
                                            intNbOfGradeForAYear = u.IntNBOfGradeForAYear,
                                            strName = u.strName
                                         }
                             ).ToList();
  return branchsList;
}
编辑2

    public class Branch {
    public string strName { get; set; }
    public List<Mark> markList { get; set; }
    public double dblAverage { get; set; }
    public double? dblGoal { get; set; }
    public bool bHasGoal { get; set; }
    public int intNbOfGradeForAYear { get; set; }

    public Branch(){}

    public Branch(string branchName, double goal, int nbOfNotes)
    {
        // TODO: Complete member initialization
        this.strName = branchName;
        this.dblGoal = goal;
        bHasGoal = true;
        this.intNbOfGradeForAYear = nbOfNotes;
    }
公共类分支{
公共字符串strName{get;set;}
公共列表标记列表{get;set;}
公共双数据库{get;set;}
public double?dblGoal{get;set;}
公共布尔bHasGoal{get;set;}
公共int intNbOfGradeForAYear{get;set;}
公共分支(){}
公共分支(字符串分支名称、双目标、int nbOfNotes)
{
//TODO:完成成员初始化
this.strName=branchName;
this.dblGoal=目标;
bHasGoal=true;
this.intnbofgradeforeayear=nbOfNotes;
}

我认为您正在尝试映射未映射到Where表达式中的数据库列的属性。您必须基于映射的属性构建表达式。我认为因该错误而未映射的intUserID正在发生。希望这有帮助

public class Branch {
    public string strName { get; set; }
    public List<Mark> markList { get; set; }
    public double dblAverage { get; set; }
    public double? dblGoal { get; set; }
    public bool bHasGoal { get; set; }
    public int intNbOfGradeForAYear { get; set; }
    public int intUserID { get; set; } //map it

    public Branch(){}

    public Branch(string branchName, double goal, int nbOfNotes,int userID) // add userID
    {
       // TODO: Complete member initialization
       this.strName = branchName;
       this.dblGoal = goal;
       bHasGoal = true;
       this.intNbOfGradeForAYear = nbOfNotes;
       this.intUserID = userID; // map it
    }
}
公共类分支{
公共字符串strName{get;set;}
公共列表标记列表{get;set;}
公共双数据库{get;set;}
public double?dblGoal{get;set;}
公共布尔bHasGoal{get;set;}
公共int intNbOfGradeForAYear{get;set;}
public int intUserID{get;set;}//映射它
公共分支(){}
公共分支(stringbranchname,double-goal,int-nbOfNotes,int-userID)//添加userID
{
//TODO:完成成员初始化
this.strName=branchName;
this.dblGoal=目标;
bHasGoal=true;
this.intnbofgradeforeayear=nbOfNotes;
this.intUserID=userID;//映射它
}
}

这可能是因为intUserID参与了FK约束

CONSTRAINT [FK_tabBranchs_ToTable] FOREIGN KEY ([intUserID]) REFERENCES [dbo].[tabUser] ([intUserID])
你能试试这个,只是为了解决问题吗

where u.tabUser.intUserID.Equals(userID)

如果这样做有效,那么还需要再次检查以下查询是否没有导致不必要的联接

属性
intUserID
是否映射到数据库列?如何确定
intUserID
的值?该列是计算列还是直接来自数据库?它直接来自我的数据库?是否可以输入是否包含表的架构?我编辑了文章。我无法使用u.tabUser访问tabUser,因为该用户拥有分支。我如何在映射属性上构建表达式?在edit 2 map intUserID上。添加属性并分配它。我添加了您编写的内容,但它仍然不起作用,这是完整的解决方案吗?请尝试此解决方案:var list=db.tabBranchs.Find(intBranchID);然后处理列表。我尝试过,列表现在等于null,我不知道为什么。我验证了这两个表,列表不应该为null
where u.tabUser.intUserID.Equals(userID)