0

I have a method in which I'm trying to return some database results through a LINQ query, however Visual Studio is not allowing me to use a select statement. This is what I have so far:

public static int GetCurrentUserDepartmentId(Guid userGuid)
{
    int departmentId = -1;

    using (PTMS_DataEntities entities = new PTMS_DataEntities())
    {
        var userDepartment = from employee in entities.Employees
                             join user in entities.aspnet_Users
                             on employee.User_Id equals user.UserId
                             where employee.User_Id equals userGuid                                     

        departmentId = (int)userDepartment;                
    }

    return departmentId;
}  

However, in the LINQ segment, I would like it to be as follows:

from employee in entities.Employees
join user in entities.aspnet_Users
on employee.User_Id equals user.UserId
where employee.User_Id equals userGuid  
select employee.Department_Id  

Is there a particular reason why I'm not being allowed to add the last select portion?

Chris V.
  • 1,123
  • 5
  • 22
  • 50

2 Answers2

2

Correct your

where employee.User_Id equals userGuid 

with

where employee.User_Id == userGuid
aush
  • 2,108
  • 1
  • 14
  • 24
  • So just to clarify, == and equals work similarly in regular c# code, meaning equals is equivalent to .Equals()? Apologies, I'm somewhat new to using LINQ. – Chris V. Mar 04 '13 at 17:16
  • @Chris V., *equals* is a contextual keyword which is used only in a join clause in a query expression. – aush Mar 04 '13 at 17:19
  • i believe that equals is used only within linq as apart of the join syntax, == and .Equals are a different mechanism, i may be incorrect though – AdamWhite Mar 04 '13 at 17:19
  • Ah, okay. So it's only for use in join conditions. – Chris V. Mar 04 '13 at 17:19
2

The issue is with your where clause:

var userDepartment = from employee in entities.Employees
                         join user in entities.aspnet_Users
                         on employee.User_Id equals user.UserId
                         where employee.User_Id equals userGuid

Should be:

var userDepartment = from employee in entities.Employees
                         join user in entities.aspnet_Users
                         on employee.User_Id equals user.UserId
                         where employee.User_Id == userGuid
                         select employee.Department_Id
AdamWhite
  • 1,044
  • 2
  • 10
  • 10