New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CS0019: Operator '!=' cannot be applied to operands of type 'TEnum' and 'TEnum' where TEnum: Enum #2025
Comments
Why not just |
No, because it doesn’t say so in the function definition. What changes if you change the definition to this? private static object[] GetEnumLookupData<TEnum>() where TEnum : struct, Enum After your edit:
Not all enums are backed by |
@chrisoverzero This does not help. |
The C# compiler doesn't treat the |
Sure it did. Now you have a different problem, one caused by EF. Not all I'm not sure there’s any problem here for csharplang to solve. |
@chrisoverzero No it did not. Try the first version without That with |
@voroninp You are applying multiple solutions at once, confusing both me and the issue. To get
This frees you up to apply one of the equality checking suggestions from this thread, the instance method or the default equality comparer. How to solve the EF problem, I have no idea. An |
@chrisoverzero > the type signature I suggested will work Did you try it?! It does not compile. private static object[] GetEnumLookupData<TEnum>() where TEnum: struct, Enum
{
var data = Enum.GetValues(typeof(TEnum))
.OfType<TEnum>()
.Where(e => e != default(TEnum))
.Select(e => new {Id = e, Name = e.ToString()})
.ToArray();
return data;
} |
@chrisoverzero Ok, my implicit initial assumption was wrong. Structs do not have |
This does not compile, but returns a error:
What am I missing here? Isn't it clear that
TEnum
is a value type?The funniest thing is that if I omit
(TEnum)
indefault(TEnum)
, so it is nowe != default
it compiles, butdefault
generates porbably just anull
because I see 0 value in created EF Core migration. WTF?!I tried to use
EqualityComparer<TEnum>.Default.Equals(...)
and it kinda worked, however, EF Core 2.1 has a bug: migrations with seeding of data with emun as primary key are unstable. This bug is fixed only in EF Core 2.2 preview.So I changed to
int
and ...The text was updated successfully, but these errors were encountered: