无法将 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)));
当我运行此代码时出现错误:
无法将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ForEach()
的类型为void
。Select()
返回IEnumerable
,ToList()
返回List
等.:
或者
因为您无法将
void
分配给List
。或者
ForEach()
has typevoid
.Select()
returnsIEnumerable<T>
,ToList()
returnsList<T>
, etc.so:
or
because you can't assign
void
toList<T>
.or
ForEach()
不返回任何内容。它的类型是空的。尝试将 ForEach() 调用替换为 选择()。
ForEach()
does not return anything. Its type is void.Try replacing your ForEach() calls with Select().
我看到了您的其他问题,其中您针对同一查询提出了类似的问题。代码部分如下所示:
看起来您已经使用了答案,并且您正在尝试将查询返回的值分配给变量“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 :
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.