Using type alias in linq expression generates error

For some reason, this statement works fine:

vms.Where(vm => vm.MessageType == ValidationMessage.EnumValidationMessageType.Warning) 

But if at the top of the class I define an alias (to save space):

 using MsgType = ValidationMessage.EnumValidationMessageType; 

Then the resulting line of code:

 vms.Where(vm => vm.MessageType == MsgType.Warning) 

Gives me an error:

enter image description here The β€œdelegate” System.Func<ValidationMessage, int, bool> 'does not accept 1 argument. ”What is strange is that this is not the delegate that I use. I use the overload System.Func<ValidationMessage, bool> ' .Where<>() is the same as when I did not use an alias.

Note that wherever an alias is used, it works fine, only inside these linq delegates it breaks. Why is this happening?

+6
source share
1 answer

When I tried to run my program, all these errors were deleted, and one error appeared with a complaint about the declaration of an alias of type.

The problem was that the type ValidationMessage.EnumValidationMessageType existed in the namespace that was declared later:

 using WPF.Utilities.ObjectModel; using MsgType = ValidationMessage.EnumValidationMessageType; 

Of course, C # cannot figure out where the type comes from based on previous namespace inclusions, I had to fully expose it:

 using WPF.Utilities.ObjectModel; using MsgType = WPF.Utilities.ObjectModel.ValidationMessage.EnumValidationMessageType; 

As soon as I did this, another problem disappeared.

I assume that I was so carried away and confused by the strange errors coming out of the linq operator, coupled with the fact that VS did not detect errors when I used the alias in the triple operators above, that I did not see an obvious error there.

Thanks for the nemesv hint - I should know better than trusting the development-time compiler.

+2
source

Source: https://habr.com/ru/post/919268/


All Articles