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

发布于 2024-12-17 18:33:13 字数 628 浏览 0 评论 0原文

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

当我运行此代码时出现错误:

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

我用 google 搜索并发现这是一个类型问题,因为 foreach 不是 LINQ 函数?我无法理解 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()
                        })
           .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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

面如桃花 2024-12-24 18:33:13
  • ForEach() 的类型为 void

  • Select() 返回 IEnumerableToList() 返回 List 等.

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

或者

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

因为您无法将 void 分配给 List


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

或者

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

foreach (var ip in query)
{
    PAINTIP(ip);
}
  • 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);
}
稚然 2024-12-24 18:33:13

ForEach() 不返回任何内容。它的类型是空的。

尝试将 ForEach() 调用替换为 选择()

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

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

忘东忘西忘不掉你 2024-12-24 18:33:13

我看到了您的其他问题,其中您针对同一查询提出了类似的问题。代码部分如下所示:

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

看起来您已经使用了答案,并且您正在尝试将查询返回的值分配给变量“Q”。查看您之前的帖子: LINQ IEnumerable中的语法

正如其他人所说,ForEach 返回类型为“void”。一旦使用集合初始化变量“Q”,您应该调用“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". 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文