如何将IQueryable列表合并到c#中另一个现有的IQueryable源中?

时间:2015-06-30 11:20:50

标签: c# linq

以下是我的代码。

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

场景是:我有一个列表并使用它,我想从IQueryable源(this.Context.ACRViewCars)中检索数据。如果找到数据,我想将记录合并到一个IQueryable对象(数据)中。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

如果您无法使用IQueryable<T>,请执行以下操作:

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

答案 1 :(得分:-1)

虽然有duplicates可以回答您的特定问题,但为什么不在一次点击中执行此操作而不是合并多个结果?

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

假设查询提供程序正在处理SQL,通常会将其转换为:

SELECT StatusId, x, y, z
FROM ACRViewCars
WHERE StatusId IN (1, 4, 6);
相关问题