简体   繁体   English

MVC4 Linq查询优化

[英]MVC4 Linq Query Optimisation

I have the below code which works but I do not feel this is the best way to achieve the result. 我有以下有效的代码,但我不认为这是获得结果的最佳方法。 I am looking at optimising my code. 我正在优化我的代码。 Any suggestions of a better option will be appreciated. 任何更好的选择的建议将不胜感激。 sub is a subcategory which is nullable. sub是可为空的子类别。

[AllowAnonymous]
    public ActionResult _relatedgrps(string cat, string sub)
    {
         if (!string.IsNullOrWhiteSpace(sub)){
            var pgs = db.Pages
            .Where(u=>u.MetaNoSearch==false)
            .Where(u => u.PaOk == true && u.Category.Name == cat &&   u.SubCategory.CatName == sub) 
            .OrderByDescending(u => u.PaCreatedOn);
        return PartialView(pgs.ToList());

         }else{
           var pgs = db.Pages
            .Where(u=>u.MetaNoSearch==false)
            .Where(u => u.PaOk == true && u.Category.Name == cat ) 
          .OrderByDescending(u => u.PaCreatedOn);   
        return PartialView(pgs.ToList());

    }}

Linq IEnumerables can be additive and the query will only be executed when enumerated for the first time (like calling .ToList() ). Linq IEnumerables可以加法,并且仅在第一次枚举时才执行查询(如调用.ToList() )。 So you should be able to do something like this: 因此,您应该能够执行以下操作:

var pgs = db.Pages
    .Where(u => u.MetaNoSearch == false &&
           u.PaOk == true &&
           u.Category.Name == cat);

if (!string.IsNullOrWhiteSpace(sub))
{
    pgs = pgs.Where(u => u.SubCategory.CatName == sub);
}

return PartialView(pgs.OrderByDescending(u => u.PaCreatedOn).ToList());

@user3021830 - be careful with String.IsNullOrWhitespace, you cannot use that in a database query. @ user3021830-注意String.IsNullOrWhitespace,您不能在数据库查询中使用它。 You could do String.IsNullOrWhitespace(sub), but not String.IsNullOrWhitespace(u.*). 您可以执行String.IsNullOrWhitespace(sub),但不能执行String.IsNullOrWhitespace(u。*)。

I'd avoid any conditionals in the query because that will likely result in a case statement in the SQL. 我会避免在查询中使用任何条件,因为这可能会导致在SQL中产生case语句。

To produce the best SQL I'd do something like this: 为了产生最好的SQL,我会做这样的事情:

var pgs = db.Pages.Where(u => u.MetaNoSearch == false)
                    .Where(u => u.PaOk == true)
                    .Where(u => u.Category.Name == cat);

if (!string.IsNullOrWhiteSpace(sub))
{
    pgs = pgs.Where(u => u.SubCategory.CatName == sub);   
}

var result = pgs.OrderByDescending(u => u.PaCreatedOn).ToList();

Create an object to query it. 创建一个对象以对其进行查询。 To improve it, you also could remove it boolean comparations because they are conditions. 为了改善它,您也可以删除它的布尔比较,因为它们是条件。

var query = db.Pages.Where(u => !u.MetaNoSearch && u.PaOk && u.Category.Name == cat);

if (!string.IsNullOrWhiteSpace(sub))
    query = query.Where(u => u.SubCategory.CatName == sub);

query = query.OrderByDescending(u => u.PaCreatedOn);

return PartialView(query.ToList());

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

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