Anele Ngqandu
Anele Ngqandu

Reputation: 125

Filter linq query results using values from a list

I am trying to filter my query results jobs. I am using the passed List parameter types trying to filter jobs. The types comes from Multiple check boxes that are selected. The issue is I am getting wrong Jobs results here foreach (var type in types). I only get the results of the last item in the foreach.

public JobsOutPut GetJobs(List<string> types, string country)
{
    //Getting the jobs here
    var jobs =
    (
        from row in _jobCategoryRepository.GetAll().ToList()
        join rowT in _jobTypeRepository.GetAll().ToList()
        on row.JobId equals rowT.JobId
        orderby row.CreationTime descending
        select new JobDto
        {
            Id = row.JobId,
            Title = row.Job.Title,
        }
    )
        .AsEnumerable()
        .Where(j => j.Country.Contains(country));

    if (types.Any())//if that List has stuff filter jobs using those values.
    {
        //is there a better approach leading to correct results?I ran out of ideas 
        foreach (var type in types)
        {
            jobs = jobs.Where(j => j.TypeName == type);

        }
    }

    var fJobs = jobs.ToList();
    return new FilterJobOutPut
    {
        JobsList = fJobs,
    };
}

Upvotes: 1

Views: 370

Answers (2)

Enigmativity
Enigmativity

Reputation: 117064

It seems to me that you want this:

public JobsOutPut GetJobs(List<string> types, string country)
{
    //Getting the jobs here
    var jobs =
    (
        from row in _jobCategoryRepository.GetAll().ToList()
        join rowT in _jobTypeRepository.GetAll().ToList() on row.JobId equals rowT.JobId
        let jobDto = new JobDto
        {
            Id = row.JobId,
            Title = row.Job.Title,
        }
        where jobDto.Country.Contains(country)
        where !types.Any() || types.Any(t => jobDto.TypeName == t)
        orderby row.CreationTime descending
        select jobDto
    );

    return new FilterJobOutPut
    {
        JobsList = jobs.ToList(),
    };
}

Or if you want to get more fancy, this:

public JobsOutPut GetJobs(List<string> types, string country) =>
    new FilterJobOutPut
    {
        JobsList =
        (
            from row in _jobCategoryRepository.GetAll().ToList()
            join rowT in _jobTypeRepository.GetAll().ToList() on row.JobId equals rowT.JobId
            let jobDto = new JobDto
            {
                Id = row.JobId,
                Title = row.Job.Title,
            }
            where jobDto.Country.Contains(country)
            where !types.Any() || types.Any(t => jobDto.TypeName == t)
            orderby row.CreationTime descending
            select jobDto
        ).ToList()
    };

Upvotes: 2

Lucifer
Lucifer

Reputation: 1594

Use Contains() to make your code more simple,just make sure case of j.TypeName && contents of types should be same

if (types.count >0)
{
      return new FilterJobOutPut
      {
        JobsList = jobs.Where(j => types.Contains(j.TypeName)).ToList()
      };
}
else
    return null //make null check where ever you call this method else use new FilterJobOutPut()

Upvotes: 3

Related Questions