An implicitly-typed local variable cannot be set to void.

c# compiler-errors entity-framework linq linq-to-entities

Question

var query = rep.GetIp()  // in this line i have the error
           .Where(x => x.CITY == CITY)
           .GroupBy(y => o.Fam)
           .Select(z => new IpDTO
                        {
                            IId = z.Key.Id,
                            IP = z.Select(x => x.IP).Distinct()
                        })
           .ToList().ForEach(IpObj => IpObj.IP.ToList().ForEach(ip => PAINTIP(ip)));

When I run this code I have the error:

Cannot assign void to an implicitly-typed local variable

I googled and found that it is a type issue because foreach is not a LINQ function? I cannot understand where the void is!

1
14
10/30/2015 7:28:35 PM

Accepted Answer

  • ForEach() has type void.

  • Select() returns IEnumerable<T>, ToList() returns List<T>, etc.

so:

List<X> x = ...Select(x => x).ToList(); // List<T>

or

x.ForEach(x => x); // void

because you can't assign void to List<T>.


var query = rep.GetIp()  // in this line i have the error
           .Where(x => x.CITY == CITY)
           .GroupBy(y => o.Fam)
           .Select(z => new IpDTO
                        {
                            IId = z.Key.Id,
                            IP = z.Select(x => x.IP).Distinct()
                        });

foreach (var dto in query)
{
    foreach (var ip in dto.IP)
    {
        PAINTIP(ip);
    }
}

or

var query = ....
           .SelectMany(z => z.Select(x => x.IP).Distinct());

foreach (var ip in query)
{
    PAINTIP(ip);
}
13
11/23/2011 2:10:10 PM

Popular Answer

ForEach() does not return anything. Its type is void.

Try replacing your ForEach() calls with Select().



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow