简体   繁体   English

LINQ:如何使用IQueryable()选择特定列

[英]LINQ: How to Select specific columns using IQueryable()

I need to select only two columns from Hospital table, HospitalId and Name. 我只需从Hospital表,HospitalId和Name中选择两列。 i tried the below code it selects all columns from Hospital table which lead to slow performance. 我尝试下面的代码,它选择医院表中的所有列,导致性能下降。 Please help me to select only two columns from Hospital table 请帮我从医院表中选择两列

public HttpResponseMessage GetAvailableHospitalsByAjax(System.Guid? DirectorateOfHealthID = null, System.Guid? UnitTypeID = null, string DeviceTypeIDs = null)
{
    Context db = new Context();
    var query = db.Hospitals.AsQueryable();
    if (UnitTypeID != null)
    {
        query = query.Where(j => j.HospitalDepartments.Any(www => www.Units.Any(u => u.UnitTypeID == UnitTypeID)));
    }

    if (DirectorateOfHealthID != null)
    {
        query = query.Where(h => h.DirectorateHealthID == DirectorateOfHealthID);
    }


    query = query.Where(j => j.HospitalDepartments.Any(u => u.Units.Any(d => d.Devices.Any(s => s.Status == Enums.DeviceStatus.Free)))
    && j.HospitalDepartments.Any(hd => hd.Units.Any(u => u.Beds.Any(b => b.Status == Enums.BedStatus.Free))));



    var list = query.ToList().Select(w => new HospitalInfo()
    {
        Id = w.ID,
        Name = w.Name 

    }).ToList();


    return Request.CreateResponse(HttpStatusCode.OK, list);
}

Remove the ToList call before the projection: 在投影之前删除ToList调用:

  var list = query.Select(w => new HospitalInfo()
  {
     Id = w.ID,
     Name = w.Name 

  }).ToList();

With that ToList call you are materializing your query before do the projection 通过该ToList调用,您可以在进行投影之前实现查询

IQueryable<T> executes select query on server side with all filters. IQueryable<T>使用所有过滤器在服务器端执行选择查询。 Hence does less work and becomes fast. 因此,工作量减少,变得快速。

IEnumerable<T> executes select query on server side, load data in-memory on client side and then filter data. IEnumerable<T>在服务器端执行select查询,在客户端加载内存中的数据,然后过滤数据。 Hence does more work and becomes slow. 因此,做更多的工作,变得缓慢。

List<T> is just an output format, and while it implements IEnumerable<T> , is not directly related to querying. List<T>只是一种输出格式,虽然它实现了IEnumerable<T> ,但它与查询没有直接关系。

So, 所以,

var list = query.ToList().Select(w => new HospitalInfo()
    {
        Id = w.ID,
        Name = w.Name 

    }).ToList();

In your code you use query.ToList() . 在您的代码中,您使用query.ToList() This means at first it pull all data into memory then apply Select query.If you want to retrieve HospitalID and Name then remove ToList() then your code like 这意味着首先它将所有数据拉入内存然后应用Select查询。如果你想检索HospitalID和Name然后删除ToList()然后你的代码就像

   var list = query.Select(w => new HospitalInfo
        {
            Id = w.ID,
            Name = w.Name     
        }).ToList();

Because you do query.ToList() this materialises the entire query with all columns into memory. 因为你执行query.ToList()所以将包含所有列的整个查询实现到内存中。 It's actually a bad habit to get into. 这实际上是一个坏习惯。 Instead, remove that, you already have it at the end anyway. 相反,删除它,无论如何你已经在最后。 The Select projection you have will only retrieve the relevant columns though: 您拥有的Select投影仅检索相关列:

var list = query.Select(w => new HospitalInfo()
{
    Id = w.ID,
    Name = w.Name 

}).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM