Error with implicitly typed variables
When assigning a linq selection to an implicitly typed local variable "var" i receive the following error.
Error :Cannot assign method group to an implicitly-typed local variable
at
root开发者_JS百科 : var mailgroup = emails.Where(p =>IsValidFormat(p.Value)).Select;
Dictionary of elements
Dictionary<int, string> emails = new Dictionary<int, string>();
emails.Add(1, "Marry@yahoo.com");
emails.Add(2, "Helan@gmail.com");
emails.Add(3, "Rose");
emails.Add(4, "Ana");
emails.Add(5, "Dhia@yahoo.com");
Dictionary<int, string> dc = new Dictionary<int, string>();
How to correct it ?
What are you expecting it to do? You probably want to make an actual method call there, such as:
var mailgroup = emails.Where(p =>IsValidFormat(p.Value))
.Select(p => p.Value);
Or if you just want the key/value pairs, you can just use:
var mailgroup = emails.Where(p =>IsValidFormat(p.Value));
and remove the "Select" entirely.
If you do just want the values (as per the first code snippet) I'd suggest using:
var mailgroup = emails.Values.Where(p =>IsValidFormat(p));
Without any brackets, your reference to "Select" is a method group - the kind of thing you'd convert to a delegate, e.g.
Func<int> x = SomeMethod; // Where SomeMethod is declared as "int SomeMethod()"
It doesn't really make sense to use Select
as method group in this case, although it is just about feasible...
You're missing ()
after Select
. As a result, what's being assigned to the variable is a reference to the Select method, which the compiler refers to as a 'method group'.
Incidentally, based on the code that you've posted I don't think that you need the trailing .Select()
at all.
精彩评论