Search menu with multiple options if(selectedGroup == 0 &amp;&amp; model.selectedParameterItem != null)
    {
        foreach (var i in model.selectedParameterItem)
        {
            parameters.Add(db.ParametersMethods.Where(f =&gt; f.ID == i).FirstOrDefault());
        }
        methods.AddRange(methodlogicclass.GetMethodFromParameters(parameters));
        ViewBag.methods = convertMethod(method);

        return View(methodModel);
    }

    else if (selectedGroup !=0 &amp;&amp; model.selectedParameterItem == null)
    { 
        methods = db.Methods.Where(f =&gt; f.MethodGroupID == selectedGroup).ToList();
        ViewBag.methods = convertMethod(methods);

        return View(methodModel);
    }

    else if(selectedGroup != 0 &amp;&amp; model.selectedParameterItem != null)
    {
        List&lt;Methods&gt; groupMethods = db.Methods.Where(f =&gt; f.MethodID == selectedGroup).ToList();
        foreach (var i in model.selectedParameterItem)
        {
            parameters.Add(db.ParametersMethods.Where(f =&gt; f.ID == i).FirstOrDefault());
        }
        List&lt;Methods&gt; parameterMethods = methodlogicclass.GetMethodsFromParameters(parameters);



        foreach(var methods1 in groupMethods)
        {
            foreach(var methods2 in parameterMethods)
            {
                if(methods1.ID == methods2.ID)
                {
                    methods.Add(method2);
                }
            }
        }

        ViewBag.methods = convertMethods(methods);

        return View(methodModel);
    }


So I'm trying to create a searchmenu, with multiple options that don't necessarily have to be selected. However, search results obviously have to meet every option selected.

What would be the best way to write code like this, with the least amount of duplication, while keeping into account that model.selectedParameterItem (for instance) can be null and thus can't be used. Right now the code isn't closed for modification at all, which is far from optimal.

WHat would be best practice in a case like this? Check for each element if it is null, then get the results for each of them seperatly, then merge all the results?

Code explained:


Search by group => search for methods by group
Search by parameter => search for methods by parameters
Etc, more will be added
When searching for both, only return the methods that meet both group and parameter requirement (and in the future, other requirements).
Group can be empty (0), list of parameters can be empty (null).
Future added requirements will be able to be empty (null)
Use English language for naming. .NET uses English for naming. You have mix of languages, and code looks like obfuscated for those who don't speak German.
Use descriptive names. I don't know German, but wvl, wek1, wek2 is definitely not very descriptive.
You have mix of presentation-related and data access code. You should split this code.


So, the first part is your controller code. It should look like technical specification when you read it. Let's try to do that

var methods = GetMethods(selectedGroupId, model.selectedParameterItem);
var model = ConvertMethodsToModel(methods);
return View(model);


Pretty clean. Now lets think about GetMethods method. It's purpose is returning some methods depending on what was found in methods group and parameter methods. Btw it can be method of another class (e.g. some service)

private IEnumerable&lt;Method&gt; GetMethods(int groupId, IEnumerable&lt;int&gt; parameterIds)
{
   var none = Enumerable.Empty&lt;Method&gt;();
   var groupMethods = (groupId == 0) ? none : GetMethodsByGroup(groupId);
   var parameterMethods = (parameterIds == null) ? none : GetParameterMethods(parametrIds);

   if (!parameterMethods.Any())
       return groupMethods;

   if (!groupMethods.Any())
       return parameterMethods;

   var parameterMethodsFromGroup =
        from pm in parameterMethods
        join gm in groupMethods on pm.Id equals gm.Id
        select pm;

   return parameterMethodsFromGroup;
}


And last part - those GetMethodsByGroup and GetParameterMethods (which could be moved to corresponding repositories):

private IEnumerable&lt;Method&gt; GetMethodsByGroup(int groupId)
{
    return db.Methods.Where(m =&gt; m.MethodGroupID == groupId).ToList();
}


Getting parameter methods:

private IEnumerable&lt;Method&gt; GetParameterMethods(IEnumerable&lt;int&gt; parameterIds)
{
    var parameterMethods = db.ParametersMethods.Where(pm =&gt; parameterIds.Contains(pm.ID));
    return methodlogicclass.GetMethodFromParameters(parameterMethodss).ToList();
}