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)));

I get the following problem when I execute this code:

Cannot assign void to an implicitly-typed local variable

I looked it up on Google and learned that there is a type problemforeach Not a LINQ function, is it? I'm unable to comprehend where thevoid is!

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

Accepted Answer

  • ForEach() hath kindvoid .

  • 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

given that you cannot assignvoid 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 provide any results. It has a void type.

Try calling Select() instead of ForEach().



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