-1

Below is my code.

public IQueryable GetCarsByStatusId(List<int> statusIds = null)
{
  IQueryable data = null;
  if(statusIds == null)
  {
    data = this.Context.ACRViewCars.OrderBy(x=>x.Status);
  }
  else if(statusIds != null && statusIds.Count > 0)
  {
    foreach(int id in statusIds)
    {
      var query = this.Context.ACRViewCars.Where(x => x.StatusId == id);
      if(query != null)
      {
        //What to do here; I have no idea
      }
    }
  }
  return data;
}

The scenario is: I have a list and using this, I want to retrieve data from IQueryable source (this.Context.ACRViewCars). And if data found, I want to merge the records in one single IQueryable object (data). Can anyone help please?

leppie
  • 115,091
  • 17
  • 196
  • 297
vpv
  • 920
  • 2
  • 20
  • 46
  • The link you are referring to, contains 2 different IQueryables (q1 & q2) and lately they were merged. But in my case, I want to obtain IQueryables using a list and the list can contains 100s or 1000s records. In that case, how should I use the referred link's solution. Please correct me if I am wrong. – vpv Jul 01 '15 at 05:14

2 Answers2

0

if you can not use IQueryable<T>, a way to do this:

public IEnumerable<IQueryable> GetCarsByStatusId(List<int> statusIds = null)
{
    if (statusIds == null)
    {
        yield return this.Context.ACRViewCars.OrderBy(x => x.Status);
    }
    else if (statusIds != null && statusIds.Count > 0)
    {
        foreach (int id in statusIds)
        {
            yield return this.Context.ACRViewCars.Where(x => x.StatusId == id);
        }
    }
}
Cologler
  • 724
  • 6
  • 18
-1

While there are duplicates that answer your specific question, why not just do this in one hit instead of merging multiple results?

return this.Context.ACRViewCars.Where(x => statusIds.Contains(x.StatusId));

Assuming the query provider is dealing in SQL, this is usually translated to something like:

SELECT StatusId, x, y, z
FROM ACRViewCars
WHERE StatusId IN (1, 4, 6);
Community
  • 1
  • 1
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • Thanks for your reply. I am already using this. However, I am marking your answer as "Answer". :) – vpv Jul 01 '15 at 05:23