HUSTLIN
HUSTLIN

Reputation: 197

Linq parameter error

I have this query

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;
}

And I have error:

A local variable named 'x' cannot be declared in this scope because it would give a different meaning to 'x', which is already used in a 'parent or current' scope to denote something else.

Why I have this error and how to solve this?

I have this model

public class TimesheetModel
{
    public DateTime TaskDate { get; set; }
    public IEnumerable<TimesheetListModel> List { get; set; }

    public TimesheetModel() { List = new List<TimesheetListModel>(); }
}

And

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; }

    }

And my task is filtering by UserId this model

{
    "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
        }
    ]
}

Upvotes: 1

Views: 93

Answers (3)

Nikita Shrivastava
Nikita Shrivastava

Reputation: 3018

you were using x as variable inside two lambda expressions(obj.Where, that is parent scope & list.Where, which is current scope),try this:

   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;
    }

Upvotes: 4

Rahul Singh
Rahul Singh

Reputation: 21795

Error message is self explanatory, you need different variable there:-

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

Also, you query should be using Any instead of Where when comparing UserId:-

 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(); 

Upvotes: 2

Neel
Neel

Reputation: 11721

You need a diferent variable than x as below:

 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();

Upvotes: 1

Related Questions