0

I am just looking if there is a better way to do the following.

Say I have the following strings:

string fooWeb = "web";
string fooDesktop = "desktop";
string fooMac = "mac";
string fooIphone = "iphone";
string fooAndroid = "android";


list<string> Names = new List<string>();
Names.Add("web");
Names.Add("mac");
Names.Add("iphone");
Names.Add("android");

I am looping through this list and i want to exclude the item which contains for example "web" & "mac" & and "android" there could be others too.

Is there a better way to achieve this than what I am trying below:

foreach (var item in this._repo.FooRepo())
if (!(item.SystemName == "Web" || item.SystemName == "android" || item.SystemName == "mac"))
{
  //add to new list
}
}

I am asking, as I have around 8 strings to check against.

Harry
  • 3,031
  • 7
  • 42
  • 67
  • Would a regular expression work? – user1620220 Dec 19 '16 at 17:25
  • 1
    better in what sense? better so that you don't need to list all the cases or that you want to have shorter code? For the latter you could use LINQ: `var list = this._repo.FooRepo().Except(new List { "Web", "android", "mac"});` but this creates additional list object which is not really needed. – Paweł Łukasik Dec 19 '16 at 17:29
  • I guess the latter as it seems cumbersum to write all these out in the if statement, all the answers below are valid suggestions. I prefer this linq with .Except. I was looking for something like this. – Harry Dec 19 '16 at 19:06

3 Answers3

1

You could through them into an array and use the .Contains() method.

var validItems = new [] { "Web", "android", "mac" };

foreach (var item in this._repo.FooRepo())
{
    if (!validItems.Contains(item))
    {
        //add to new list
    }
}
Matt Rowland
  • 4,575
  • 4
  • 25
  • 34
  • I know I can do that, but also wouldn't I need to do that for each string? – Harry Dec 19 '16 at 17:27
  • 1
    You would just add the string to the array. If you have a set of valid data, you are going to need that persisted somewhere. That could be hardcoded into the array, in a database, in a config file, or some other way. – Matt Rowland Dec 19 '16 at 17:28
  • thanks, i will take your points on board :) – Harry Dec 19 '16 at 19:06
  • I will accept this, as this seems appropriate for my scenerio. – Harry Dec 20 '16 at 10:42
0

First part simplified:

list<string> Names = new List<string> () {
    "web",
    "mac",
    "iphone",
    "android"
};

or:

var Names = new [] {
    "web",
    "mac",
    "iphone",
    "android"
};

or:

string[] Names = new string[] {
    "web",
    "mac",
    "iphone",
    "android"
};

Second part simplified:

myList.Add(this._repo.FooRepo().Where(i => !Names.Contains(i.SystemName)));
maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37
-1

You could create your own method to evaluate any number of strings, which can be used whenever - in a separate, static class:

public static class StringFunctions
{
    public static bool IfContains(this string inString, params string[] comparisonStrings)
    {
        return comparisonStrings.Contains(inString);
    }
}

This can be called like so

if(!item.SystemName.IfContains("web", "desktop", "mac", "iphone", "android"))
{
    //add to new list
}

You can also pass an array into the method

string[] words = { "web", "desktop", "mac", "iphone", "android" };
if(!item.SystemName.IfContains(words))
{
    //add to new list
}
Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26