如何优化我的if else代码c#

时间:2015-07-08 07:21:18

标签: c# .net model-view-controller

我的代码如下:

TryUpdateModel(model);
            if (ModelState.IsValid && model.FullNameIsChecked == true && model.LookingFor == null)
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
                model.ContactList.Sort((x, y) => string.Compare(x.FullName, y.FullName));
            }
            else if (ModelState.IsValid && model.FullNameIsChecked == false && model.LookingFor == null)
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
                model.ContactList.Sort((x, y) => string.Compare(x.Email, y.Email));
            }
            else if (ModelState.IsValid && model.LookingFor != null && model.FullNameIsChecked == true)
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
                model.ContactList = contactRepository.GetAll(filter: x => x.FullName.Contains(model.LookingFor));
                model.ContactList.OrderBy(f => f.FullName);
            }
            else if (ModelState.IsValid && model.LookingFor != null && model.FullNameIsChecked == false)
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
                model.ContactList = contactRepository.GetAll(filter: x => x.Email.Contains(model.LookingFor));
                model.ContactList.OrderBy(e => e.Email);
            }
            else
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
            }

我使用带有Razor引擎的.NET MVC 5,并使用上面的代码进行排序操作。我可以优化我的 If else 部分代码吗?我唯一想到的是使用 switch-case operator ,但它看起来几乎相似。 提前致谢

5 个答案:

答案 0 :(得分:2)

您的所有案例都适用于此:

//Place this here since it's non conditional
model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);


if(ModelState.IsValid)
{
    if(model.LookingFor == null)
    {
        if(model.FullNameIsChecked){
            //...
        }else
        {
            //...
        }
    }else
    {
        if(model.FullNameIsChecked){
            //...
        }else
        {
            //...
        }
    }
}
else
{
    //... Actually you don't need this case...
}

答案 1 :(得分:1)

  • 我无法在ModelState
  • 的代码检查状态中看到任何用途
  • 如果条件不需要比较bool,  即

    if(true)
    

    相同
    if(bool variable=true)
    
  • 使用String.IsNullOrEmpty检查null

     TryUpdateModel(model);
    
      model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
    
            if(  String.IsNullOrEmpty(model.LookingFor))
            {
                if(model.FullNameIsChecked)
                {
                    model.ContactList.Sort((x, y) => string.Compare(x.FullName, y.FullName));
                }
                else
                {
                    model.ContactList.Sort((x, y) => string.Compare(x.Email, y.Email));
                }
            }
            else
            {
                if(model.FullNameIsChecked)
                {
                   model.ContactList = contactRepository.GetAll(filter: x => x.FullName.Contains(model.LookingFor));
                   model.ContactList.OrderBy(f => f.FullName);
                }
                else
                {
                   model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
                   model.ContactList = contactRepository.GetAll(filter: x => x.Email.Contains(model.LookingFor));
                }
            }
    

答案 2 :(得分:1)

model.LookingFor非空时,您拨打contactRepository.GetAll()两次。对contactRepository.GetAll(filter: x => x.UserId == user.Id)的第一个电话是多余的,对吧?

有时您拨打.Sort(),有时则拨打.OrderBy()。为什么不一致?

您已使用xfe作为虚拟变量。我建议使用一些虚拟变量 - 也许c用于“联系”。

在任何情况下,此代码都希望执行某种GetAll(),然后可能会进行排序。所以,让我们给GetAll()打一个电话,给OrderBy()打个电话。

TryUpdateModel(model);
model.ContactList = contactRepository.GetAll(filter:
    !ModelState.IsValid ?      (c => c.UserId == user.Id) :
    model.LookingFor == null ? (c => c.UserId == user.Id) :
    model.FullNameIsChecked ?  (c => c.FullName.Contains(model.LookingFor)) :
                               (c => c.Email.Contains(model.LookingFor))
);
if (ModelState.IsValid)
{
    model.ContactList.OrderBy(model.FullNameIsChecked ?
        (c => c.FullName) : (c => c.Email)
    );
}

答案 3 :(得分:0)

TryUpdateModel(model);
if(ModelState.IsValid)
{
  model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
  if(model.LookingFor == null)
  {
        if(model.FullNameIsChecked)
        {
             model.ContactList.Sort((x, y) => string.Compare(x.FullName, y.FullName));
        }
        else
        {
             model.ContactList.Sort((x, y) => string.Compare(x.Email, y.Email));
        }
  }
  else
  {
        if(model.FullNameIsChecked)
        {
            model.ContactList = contactRepository.GetAll(filter: x => x.FullName.Contains(model.LookingFor));
            model.ContactList.OrderBy(f => f.FullName);
        }
        else
        {
            model.ContactList = contactRepository.GetAll(filter: x => x.Email.Contains(model.LookingFor));
            model.ContactList.OrderBy(e => e.Email);
        }
  }
}

答案 4 :(得分:0)

嗯,这样每个条件都只检查一次。

if (ModelState.IsValid)
{
    if (string.IsEmptyOrWhitespace(model.LookingFor))
    {
        model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
    }
    else
    {
        model.ContactList = contactRepository.GetAll(
                filter: x => x.FullName.Contains(model.LookingFor));
    }

    if(model.FullNameIsChecked)
    {
        model.ContactList.OrderBy(f => f.FullName);
    }
    else
    {
        model.ContactList.OrderBy(e => e.Email); 
    }
}
else
{
    model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
}