Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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
C# Linq where在当前上下文中不存在_C# - Fatal编程技术网

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是的,我是这样想的,但无法理解。感谢应该是固定的。。。虽然,它并不像人们希望的那样整洁。是的,是的,我是这样想的,但我想不出来。感谢应该是固定的。。。尽管如此,它并不像人们希望的那样整洁。