Error with implicitly typed variables

When assigning a linq choice to the implicitly typed local variable "var", I get the following error.

Error :Cannot assign method group to an implicitly-typed local variable

in

root : 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 fix it?

+3
source share
2 answers

What do you expect from this? You probably want to make an actual method call, for example:

var mailgroup = emails.Where(p =>IsValidFormat(p.Value))
                      .Select(p => p.Value);

Or, if you just need key / value pairs, you can simply use:

var mailgroup = emails.Where(p =>IsValidFormat(p.Value));

and completely remove the "Select".

If you just need the values ​​(according to the first code snippet), I would suggest using:

var mailgroup = emails.Values.Where(p =>IsValidFormat(p));

Without any brackets, your “Select” link is a group of methods — what you convert to a delegate, for example.

Func<int> x = SomeMethod; // Where SomeMethod is declared as "int SomeMethod()"

Select , ...

+12

() Select. , , , Select, " ".

, , , , .Select() .

+9

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


All Articles