score:11
static public bool IsLightFile(string fileName)
{
//Use any with names returned from the enum.
return Enum.GetNames(typeof(LightFiles)).Any(w => w == fileName);
}
score:1
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()
score:1
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);
}
score:1
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.
score:2
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.
Source: stackoverflow.com
Related Query
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Syntax to execute code block inside Linq query?
- Something better than .ToArray() to force enumeration of LINQ output
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Best open source LINQ provider
- Is there a good source that gives an overview of linq optimizations?
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Linq To Entities 'Only primitive types or enumeration types are supported' Error
- LINQ WHERE method alters source collection
- Entity Framework Linq Query to List - Error when using contains: Only primitive types, enumeration types and entity types are supported
- Where can I view LINQ source code?
- Suggestions for designing complex LINQ code
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ query and sub-query enumeration count in C#?
- Left outer join using LINQ -- understanding the code
- How to pass LinQ Expressions from F# to C# code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- How does linq actually execute the code to retrieve data from the data source?
- How can I switch that code to use LINQ
More Query from same tag
- LINQ query, using relationships
- Is there a way to use an object from a list only if it exists?
- Get node value xml with LINQ c#
- Is a dynamic pivot using LINQ possible?
- dynamically specify select columns linq
- Get Fields From not typed <Ienumerable>Object c# linq
- Is there any pure sql way to delete data from log table in following ways
- Which method is better for multiple join and left join by Linq?
- Is it possible to have fall-back for empty values in a LINQ query considering locale?
- Poor performance comparing collections of objects using reflections and Linq Except/Intersect
- model changed, update database using codefirst
- How do you write a LINQ query that filters a sub table to a specific time period and sums the results of the sub table?
- Linq query to find super set with checks
- Chaining Linq Where clauses
- Is it possible to use an IEnumerable as a private class-level variable?
- Getting averages from different table in Entity Framework
- Return 0 if no duplicates found from DistinctBy
- Get data from database using linq query and display it in dynamically generated labels in repeater
- Enumerable.Last() Throws InvalidOperationException "Sequence contains no matching element"
- LINQ merge 2 query results
- Linq to SQL assistance with joins
- Unable add an entity that already exists
- Can I optimize this query any further?
- Use value of List<int> inside IN() clause
- Linq to SQL query filter with Lightswitch computed property
- Linq<string> 2 lists select without having to foreach
- Filter WhereSelectEnumerableIterator of Generic List using Linq
- EF/Linq - Sort collection before grouping
- LINQtoEntities include not retrieving nav property
- Select from two IEnumerables using LINQ