NoWar
NoWar

Reputation: 37633

LINQ & Enumeration

I have this enumeration

public enum LightFiles
    { 
        PNG,
        JPG,
        GIF,
        FLV,
        TIF,
        BMP,
        MP3,
        WAV,
        WMA,
        TXT,
        PDF,
        XML,
        DOC,
        XLS,
        DBF,
        SQL,
        CSS,
        HTM
    }

And I need to detect if the file name has one of the enum. item and return TRUE if it is.

I guess there is some solution via LINQ. I really don't klnow how to work around to resolve this problem...

Any clue guys?

static public bool IsLightFile(string fileName)
{ 
  // Needs some LINQ
}

Upvotes: 3

Views: 9111

Answers (5)

Surjit Samra
Surjit Samra

Reputation: 4662

static public bool IsLightFile(string fileName)
{
  //Use any with names returned from the enum.
   return Enum.GetNames(typeof(LightFiles)).Any(w => w == fileName);    
}

Upvotes: 11

Igby Largeman
Igby Largeman

Reputation: 16747

public bool IsLightFile(string filename)
{
    return Enum.GetNames(typeof(LightFiles)).Contains(
        Path.GetExtension(filename).Remove(0, 1).ToUpper());
}

The only way LINQ plays a part in this is the Enumberable.Contains() extension method.

See also:

Enum.GetNames()
Path.GetExtension()
String.Remove()
String.ToUpper()

Upvotes: 1

user743382
user743382

Reputation:

I don't see why you'd use LINQ here. To see if a string is a valid enum value, a slightly more natural solution than looping over Enum.GetNames's results, whether explicitly or implicitly using LINQ, would be Enum.TryParse.

string fileExtension = Path.GetExtension(fileName);
if (fileExtension.StartsWith("."))
    fileExtension = fileExtension.Substring(1);
LightFiles lightFile;
return Enum.TryParse(fileExtension, true, out lightFile);

Note: the alternative Enum.IsDefined is always case sensitive, while file extensions are usually treated as case insensitive.

Upvotes: 1

Mohit Vashistha
Mohit Vashistha

Reputation: 1904

static public bool IsLightFile(string fileName)
    {
      var extension = Path.GetExtension(fileName).Remove(0, 1);
      return Enum.GetNames(typeof(LightFiles)).Any(enumVal => string.Compare(enumVal, extension, true) == 0);
    }

Upvotes: 1

James Michael Hare
James Michael Hare

Reputation: 38397

A little unsure precisely what you are asking, I can see three possibilities...

If you just want the file name to contain any of the enum strings, you could do:

    static public bool IsLightFile(string fileName)
    {
        foreach (var eVal in Enum.GetNames(typeof(LightFiles)))
        {
            if (fileName.Contains(eVal))
            {
                return true;
            }
        }

        return false;
    }

Or, if you want to see if it just ends with that extension, you could do:

    static public bool IsLightFile(string fileName)
    {
        foreach (var eVal in Enum.GetNames(typeof(LightFiles)))
        {
            if (Path.GetExtension(fileName).Equals(eVal))
            {
                return true;
            }
        }

        return false;
    }

Or, if you are saying that the filename must be equal to the enum value completely, you can do:

    static public bool IsLightFile(string fileName)
    {
        return Enum.IsDefined(typeof(LightFiles), fileName);
    }

Obviously, you'd want to null check, make string compare case-insensitive, etc as necessary.

Upvotes: 2

Related Questions