简体   繁体   English

如何将两个 linq 查询组合在一起以优化性能

[英]How to combine two linq queries together to optimize performance

I have the following two queries which query from the database separately.我有以下两个查询,分别从数据库中查询。

I understand that one Linq query is much better for optimising performance.我知道一个 Linq 查询对于优化性能要好得多。

Would it be possible to do the combination of the two following queries?是否可以组合以下两个查询?

result = GetAllJobModelsOrder(db.AllJobModel.Where(a => 
     a.JobTitle.Contains(searchTitle) 
  && a.locationName.Contains(searchLocation) 
  && (a.PostedDate == timesNow || a.PostedDate == time || a.PostedDate == time2 || a.PostedDate == time3 || a.PostedDate == time4 || a.PostedDate == time5 || a.PostedDate == time6 || a.PostedDate == time7))
.ToList());

result = (from app in result orderby DateTime.ParseExact(app.PostedDate, "dd/MM/yyyy", null) descending select app).ToList();

I have the following two queries which query from the database separately.我有以下两个查询,分别从数据库中查询。

No you don't.不,你没有。 The first query hits the database and passes the results to GetAllJobModelsOrder.第一个查询命中数据库并将结果传递给 GetAllJobModelsOrder。 The second query appears to be in-memory LINQ-to-Objects, filtering and sorting the results.第二个查询似乎是内存中的 LINQ-to-Objects,对结果进行过滤和排序。

I understand that one Linq query is much better for optimising performance.我知道一个 Linq 查询对于优化性能要好得多。

That is not always the case.情况并非总是如此。

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

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