简体   繁体   English

无法将void分配给隐式类型的局部变量

[英]Cannot assign void to an implicitly-typed local variable

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 无法将void分配给隐式类型的局部变量

I googled and found that it is a type issue because foreach is not a LINQ function? 我用Google搜索并发现它是一个类型问题,因为foreach不是LINQ函数? I cannot understand where the void is! 我无法理解void在哪里!

  • ForEach() has type void . ForEach()类型为void

  • Select() returns IEnumerable<T> , ToList() returns List<T> , etc. Select()返回IEnumerable<T>ToList()返回List<T>等。

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> . 因为你不能为List<T>分配void


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

ForEach() does not return anything. ForEach()不返回任何内容。 Its type is void. 它的类型是无效的。

Try replacing your ForEach() calls with Select() . 尝试使用Select()替换ForEach()调用。

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". 看起来您已经使用了答案,并且您尝试将查询中返回的值分配给变量“Q”。 Check out your previous post : syntax in LINQ IEnumerable<string> 查看以前的帖子: LINQ IEnumerable <string>中的语法

As others have said, ForEach return type is "void". 正如其他人所说,ForEach返回类型是“无效”。 You should call "ForEach", once the variable "Q" is initialized with the collection. 一旦变量“Q”与集合初始化,您应该调用“ForEach”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM