Linq参数错误

本文关键字:错误 参数 Linq | 更新日期: 2023-09-27 18:14:44

我有这个查询

public IEnumerable<TimesheetModel> FilterByUserId(IEnumerable<TimesheetModel> obj, int FilterUserId)
{
     var query = (from list in obj.Where(x => x.List.Where(x => x.UserId.Equals(FilterUserId)))
                  select new TimesheetModel
                  {
                      TaskDate = list.TaskDate,
                      List = list.List
                  }).ToList();
     return query;
}

和我有错误:

不能在此范围内声明名为'x'的局部变量,因为它会给已经在a中使用的"x"赋予不同的含义吗'父或当前'作用域来表示其他内容。

为什么我有这个错误,如何解决这个问题?

我有这个模型

public class TimesheetModel
{
    public DateTime TaskDate { get; set; }
    public IEnumerable<TimesheetListModel> List { get; set; }
    public TimesheetModel() { List = new List<TimesheetListModel>(); }
}

public class TimesheetListModel
    {
        public DateTime Date        { get; set; }
        public bool?    InProgress  { get; set; }
        public string   Note        { get; set; }
        public int      ProjectId   { get; set; }
        public string   ProjectName { get; set; }
        public string   Task        { get; set; }
        public decimal? TimeWorked  { get; set; }
        public int?     Type        { get; set; }
        public int      UserId      { get; set; }
        public string   UserName    { get; set; }
        public int?     WorkItemId  { get; set; }
    }

我的任务是通过UserId过滤这个模型

{
    "TaskDate": "2015-01-04T00:00:00",
    "List": [
        {
            "WorkItemId":24,
            "ProjectId":3,
            "ProjectName":"Hello world",
            "UserId":12,
            "UserName":"Anatoliy Svetliakov",
            "Date":"2015-01-04T22:00:00",
            "Task":"#34 : New task test",
            "TimeWorked":2,
            "Note":null,
            "InProgress":false
        }
    ]
}

Linq参数错误

您使用x作为变量在两个lambda表达式(obj。其中,是父作用域&列表。其中,这是当前作用域),尝试如下:

   public IEnumerable<TimesheetModel> FilterByUserId(IEnumerable<TimesheetModel> obj, int FilterUserId)
    {
         var query = (from list in obj.Where(z=>z.List.Any(u=>u.UserId==FilterUserId))
                      select new TimesheetModel
                      {
                          TaskDate = list.TaskDate,
                          List = list.List.Where(o=> o.UserId.Equals(FilterUserId)).FirstOrDefault()
                      }).ToList();
         return query;
    }

错误信息是不言自明的,你需要不同的变量:-

from list in obj.Where(x => x.List.Where(z => z.UserId.Equals(FilterUserId)))

另外,当比较UserId时,您的查询应该使用Any而不是Where:-

 var query = (from list in obj.Where(x => x.List.Any(z => z.UserId.Equals(FilterUserId)))
                  select new TimesheetModel
                  {
                      TaskDate = list.TaskDate,
                      List = list.List
                  }).ToList(); 

您需要一个不同于x的变量,如下所示:

 var query = (from list in obj.Where(x => x.List.Where(y => y.UserId.Equals(FilterUserId)))
                  select new TimesheetModel
                  {
                      TaskDate = list.TaskDate,
                      List = list.List
                  }).ToList();