1

This is a little bit of a weird question, but this has me stumped for some reason. I have the below Extension function which receives 4 string parameters which will contain one of 3 values ("Match", "No Match", "Empty"). Within the function I have an if statement in which I need the condition to contain only the parameters that contain value "Match".

public static void AddIfNotNullAndInAdvancedSearch(
    this List<TreeDocument> docs, 
    TreeDocument value, 
    string studytitlecontainsshearch, 
    string outputtitlecontainssearch, 
    string datasetnamecontainssearch, 
    string templatenamecontainssearch)
{
    if (value.Path != null && 
        (studytitlecontainsshearch == "Match" && 
         outputtitlecontainssearch == "Match" && 
         datasetnamecontainssearch == "Match" && 
         templatenamecontainssearch == "Match")) 
    { 
        docs.Add(value); 
    }
}

With my code above, if for example studytitlecontainsshearch is "Empty", then the if statement resolves to false. However, in this scenario I only want to have the remaining 3 parameters in the statement and not ones that contain "Empty" or "No Match". So if only two parameters contain "Match", I want to have only those in the if statement.

I am probably missing a really obvious solution to this so apologies in advance.

6
  • 2
    What do you mean by "I only want to have those in the if statement"? It sounds like you probably want to create a count of how many parameters are "Match" and how many are "Empty", and use those results. (Have you considered using an enum for that instead of a string? It sounds like there are only a few options. I'd also strongly recommend that you follow .NET naming conventions, and use line breaks more liberally, but that's a different matter.) Commented Mar 28, 2018 at 8:23
  • Try to change your approach. Don't start thinking about each value combination for each parameter, think about when do you need to add value to Doc. Commented Mar 28, 2018 at 8:31
  • @Jon, I mean that I do not want to include the parameters that contain anything other than "Match" in my if statement Commented Mar 28, 2018 at 8:34
  • You are saying that you want to ignore parameters that do not match the condition. What this means is that if any of the parameters match the condition, you want to add the value. My answer below does exactly this. Commented Mar 28, 2018 at 8:38
  • @nerdalert What do you need on this condition so that you can Add(value) ? Commented Mar 28, 2018 at 8:54

3 Answers 3

3

So do you mean you want to get a true if any of the parameters is "Match"? Then you need to change your && (means 'And also') to || (means 'Or else')

if (value.Path != null && 
    (studytitlecontainsshearch == "Match" || 
     outputtitlecontainssearch == "Match" || 
     datasetnamecontainssearch == "Match" || 
     templatenamecontainssearch == "Match")) 
{ 
    docs.Add(value); 
}

Edit: As it seems you want to not add the doc if any param is "No Match", you can change it to: Another edit: In fact you also still need to check that there is a match

if (value.Path != null && 
    (studytitlecontainsshearch == "Match" || 
     outputtitlecontainssearch == "Match" || 
     datasetnamecontainssearch == "Match" || 
     templatenamecontainssearch == "Match") &&
    (studytitlecontainsshearch != "No Match" && 
     outputtitlecontainssearch != "No Match" && 
     datasetnamecontainssearch != "No Match" && 
     templatenamecontainssearch != "No Match")) 
{ 
    docs.Add(value); 
}

You could actually simplify the method using 'params' instead of specifying each parameter. This will allow you to add more search terms without changing the method signature later:

public static void AddIfNotNullAndInAdvancedSearch(
    this List<TreeDocument> docs, 
    TreeDocument value, 
    params string[] searches)
{
    if (value.Path != null && 
        searches.Any(x => x == "Match") &&
        !searches.Any(x => x == "No Match")) 
    { 
        docs.Add(value); 
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Unfortunately this won't work for my needs because if I use the OR operator then it will add to docs different docs that contain either of the matching searches.
Perhaps I should give a little more background as to when this extension is called because this may seem confusing
It definitely does
@nerdalert: Yes, your question is very confusing at the moment. At least for me.
@nerdalert I have updated this and expect that the lower solution is actually what you want, rather than your posted answer. See my comment on there for why.
|
0

One way to make this work is to make a different class:

public class SearchModel
{
    public string SearchType { get; set; }
    public string SearchResult { get; set; }
}

Then use this class for params in the AddIfNotNullAndInAdvancedSearch method like this:

public static void AddIfNotNullAndInAdvancedSearch(
        this List<TreeDocument> docs,
        TreeDocument value,
        params SearchModel[] searchCriteria)
    {
        var filteredSearchCriteria = searchCriteria
            .Where(x => x.SearchResult != "No Match" &&
                        x.SearchResult != "Empty")
            .ToList();

        if (filteredSearchCriteria.Any() && 
            filteredSearchCriteria.All(x => x.SearchResult == "Match"))
        {
            docs.Add(value);
        }
    }

Comments

0

Sorry I don't know if I understood your doubt, but I will try help you.

I see many solutions for this case, for example you can change the && to || in your if statement to compare your strings

But if you want something a little more expressive and flexible you can do something like that.

public static void AddIfNotNullAndInAdvancedSearch(this List<TreeDocument> docs, TreeDocument value, List<string> filters)
{
     if(value.Path != null && filters.Exists(x => x == "Match")) docs.Add(value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.