15
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!

maxshuty
  • 9,708
  • 13
  • 64
  • 77
Tania1990
  • 183
  • 1
  • 1
  • 7

3 Answers3

14
  • 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);
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • aba, thank you for the explaination , im not sure that i understand the syntax,do i need to add IpObj before the foreach as in your example? .ToList()IpObj.ForEach(IpObj => IpObj.IP.ToList().ForEach(ip => PAINTIP(ip))); ? – Tania1990 Nov 23 '11 at 13:34
  • btw, you can use tilde to highlight code in comments, e.g. `ToList(); IpObj.ForEach(IpObj => IpObj.IP.ToList().ForEach(ip => PAINTIP(ip)));` – abatishchev Nov 23 '11 at 13:37
  • @Tania: Describe literally what are you going to achieve, e.g. call PaintIP() for each IP for each town in rep? – abatishchev Nov 23 '11 at 13:41
  • i need to return the results "IId" and "IP" and for each ip(element from inside IP) call PAINTIP(ip) – Tania1990 Nov 23 '11 at 13:45
  • you are my hero! where do you define the "ip" in both examples u use ip without defining it – Tania1990 Nov 23 '11 at 14:07
  • @Tania1990: I've missed `var ip` in of the examples. Notice that in second example I don't use type `IpDTO` and use `SelectMany`() to select ip of each dto in united sequence – abatishchev Nov 23 '11 at 14:12
7

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

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

jrummell
  • 42,637
  • 17
  • 112
  • 171
1

I saw your other questions, wherein you have asked similar question for the same query. The code partially looks like this :

var Q = rep.GetIp()
        .Where(x => x.CITY == CITY)
        .GroupBy(y => o.Fam)
        .Select(z => new IpDTO
        {
          IId = z.Key.Id,
          IP = z.Select(x => x.IP).Distinct()
       });

Looks like you have used the answer as such and you are trying to assign the returned value from the query to the variable "Q". Check out your previous post : syntax in LINQ IEnumerable<string>

As others have said, ForEach return type is "void". You should call "ForEach", once the variable "Q" is initialized with the collection.

Community
  • 1
  • 1
Pawan Mishra
  • 7,212
  • 5
  • 29
  • 39
  • I understand it now but how to call ForEach only after the Q was initialized ? – Tania1990 Nov 23 '11 at 13:37
  • @Tania1990, simple: `var q = …; q.ForEach(…);`. But I would recommend you to use normal `foreach` instead of `List.ForEach()`. – svick Nov 23 '11 at 13:41
  • svick, please show me how would you use List.ForEach() with my syntax - i have used .ToList().ForEach( – Tania1990 Nov 23 '11 at 13:48
  • 1
    @Tania : You are increasing the scope of the original question. You can only write Linq queries, if you have proper understanding of lambda expression, system defined delegates like func<>, Action<> etc.Refer the following link for sample LINQ queries : http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b – Pawan Mishra Nov 23 '11 at 13:51