C# Linq where在当前上下文中不存在
我有如下用户和部门列表:C# Linq where在当前上下文中不存在,c#,C#,我有如下用户和部门列表: public class Users { public int id { get; set; } public string jobTitle { get; set; } public int officeLocation { get; set; } public string userPrincipalName { get; set; }
public class Users
{
public int id { get; set; }
public string jobTitle { get; set; }
public int officeLocation { get; set; }
public string userPrincipalName { get; set; }
}
public class Departments
{
public int id { get; set; }
public string department { get; set; }
public string displayName { get; set; }
}
这是我的Json
我想使用linq加入两个列表,在这里我将传递用户id
并得到部门
我试过这样做
var query = from user in Users
join dept in Departments
on user.id equals dept.id
select new
{
user.id ,
user.jobTitle,
user.officeLocation,
dept.department
}.where(id = 1) ;
但是where子句似乎不合适,比如说在当前上下文中where不存在这将起作用,您只是缺少一些s,where中有一个语法错误
(from user in Users
join dept in Departments
on user.id equals dept.id
select new
{
user.id ,
user.jobTitle,
user.officeLocation,
dept.department
}).Where(c => c.id == 1) ;
但通常情况下,Where是作为查询表达式预先完成的。这将起作用,您只是缺少一些s,Where中有一个语法错误
(from user in Users
join dept in Departments
on user.id equals dept.id
select new
{
user.id ,
user.jobTitle,
user.officeLocation,
dept.department
}).Where(c => c.id == 1) ;
但通常情况下,Where是作为查询表达式预先完成的。不允许以这种方式在查询语法中使用Where方法,那么我该如何过滤?在选择之前?是的,尝试在select语句之前添加where user.id==1,并使用==进行比较,而不是赋值=Yes,where位于select之前,并且不以句点开头。好的,让我试试。谢谢@Pavelanikhouski您不允许以这种方式在查询语法中使用where方法那么我该如何过滤?在选择之前?是的,尝试在select语句之前添加where user.id==1,并使用==进行比较,而不是赋值=Yes,where位于select之前,并且不以句点开头。好的,让我试试。谢谢@PavelAnikhouskiYes是的,我是这样想的,但无法理解。感谢应该是固定的。。。虽然,它并不像人们希望的那样整洁。是的,是的,我是这样想的,但我想不出来。感谢应该是固定的。。。尽管如此,它并不像人们希望的那样整洁。