Well, I have this:
public enum letters {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z}
And I want to do this:
letters letter = letters.a;
if(letter == a || letter == b || letter = c || //to z...) {
//countinue...
}
how can I do this with a LINQ expression?
EDIT:
I realised that I can return true directly, but if I want to except one, for example letter "d" and other letter like "g", how can I do that?
EDIT2:
I like weird answers for it, I don't like answers like "use this: "||" or something similar... so teach me how to do it with linq. IsDefined is a good way to do it :P
Thanks in advanced.
If you want to check against one as your update asks:
but if I want to excepting one, for example letter "d", how can I do that?
Then simply do
if (letter != Letters.D)
{
// not D
}
Your initial question will always be true because letter
cannot not be one of Letters
.#
To check multiple values, you can do the following
var invalidLetters = new[] { Letters.A, Letters.B };
if (invalidLetters.Contains(letter))
{
// letter is Letters.A or Letters.B
}
I realised that I can return true directly, but if I want to except one, for example letter "d", how can I do that?
You can use Where
+ Contains
:
var allLetters = Enum.GetValues(typeof(letters)).Cast<letters>();
var allButD = allLetters.Where(l => l != letters.d);
if (allButD.Contains(letter))
{
}
You can try this:
if(Enum.IsDefined(typeof(letters), letter))
{
return 1;
}
else
{
return 0;
}
Try
letters letter = letters.a;
if (Enum.IsDefined(typeof(letters), letter))
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.