You can utilise the Enum
class which can shorten your code significantly and it will be easier for future updates
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}
or with LINQ
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
Now you can add as much types as you want in your enum without even touching this method it will still work and find your newly created type.
Update
As @t3chb0t @t3chb0t pointed in the comments there is a way to avoid calling .ToLower()
on both the input string and the enum value by using the .IndexOf()
overload which accepts different comparison types :
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName =
enumNames.FirstOrDefault(x => vQuestion.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
return enumValueName != null
? (QuestionIdentifier)Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
You can utilise the Enum
class which can shorten your code significantly and it will be easier for future updates
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}
or with LINQ
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
Now you can add as much types as you want in your enum without even touching this method it will still work and find your newly created type.
Update
As @t3chb0t pointed in the comments there is a way to avoid calling .ToLower()
on both the input string and the enum value by using the .IndexOf()
overload which accepts different comparison types :
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName =
enumNames.FirstOrDefault(x => vQuestion.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
return enumValueName != null
? (QuestionIdentifier)Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
You can utilise the Enum
class which can shorten your code significantly and it will be easier for future updates
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}
or with LINQ
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
Now you can add as much types as you want in your enum without even touching this method it will still work and find your newly created type.
Update
As @t3chb0t pointed in the comments there is a way to avoid calling .ToLower()
on both the input string and the enum value by using the .IndexOf()
overload which accepts different comparison types :
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName =
enumNames.FirstOrDefault(x => vQuestion.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
return enumValueName != null
? (QuestionIdentifier)Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
You can utilise the Enum
class which can shorten your code significantly and it will be easier for future updates
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}
or with LINQ
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
Now you can add as much types as you want in your enum without even touching this method it will still work and find your newly created type.
Update
As @t3chb0t pointed in the comments there is a way to avoid calling .ToLower()
on both the input string and the enum value by using the .IndexOf()
overload which accepts different comparison types :
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName =
enumNames.FirstOrDefault(x => vQuestion.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
return enumValueName != null
? (QuestionIdentifier)Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
You can utilise the Enum
class which can shorten your code significantly and it will be easier for future updates
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}
or with LINQ
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
Now you can add as much types as you want in your enum without even touching this method it will still work and find your newly created type.
You can utilise the Enum
class which can shorten your code significantly and it will be easier for future updates
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}
or with LINQ
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
Now you can add as much types as you want in your enum without even touching this method it will still work and find your newly created type.
Update
As @t3chb0t pointed in the comments there is a way to avoid calling .ToLower()
on both the input string and the enum value by using the .IndexOf()
overload which accepts different comparison types :
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName =
enumNames.FirstOrDefault(x => vQuestion.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);
return enumValueName != null
? (QuestionIdentifier)Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
You can utilise the Enum
class which can shorten your code significantly and it will be easier for future updates
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}
or with LINQ
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
Now you can add as much types as you want in your enum without even touching this method it will still work and find your newly created type.
You can utilise the Enum
class which can shorten your code significantly and it will be easier for future updates
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}
or with LINQ
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
You can utilise the Enum
class which can shorten your code significantly and it will be easier for future updates
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
//QuestionToLower
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
foreach (var enumName in enumNames)
{
if (vQTL.Contains(enumName.ToLower()))
{
return (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumName);
}
}
return QuestionIdentifier.Andere;
}
or with LINQ
private static QuestionIdentifier IdentifyQuestion(string vQuestion)
{
string vQTL = vQuestion.ToLower();
string[] enumNames = Enum.GetNames(typeof(QuestionIdentifier));
string enumValueName = enumNames.FirstOrDefault(x => vQTL.Contains(x.ToLower()));
return enumValueName != null
? (QuestionIdentifier) Enum.Parse(typeof(QuestionIdentifier), enumValueName)
: QuestionIdentifier.Andere;
}
Now you can add as much types as you want in your enum without even touching this method it will still work and find your newly created type.