gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:54:34 27 4
gpt4 key购买 nike

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

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

Cannot assign void to an implicitly-typed local variable

我谷歌了一下,发现这是一个类型问题,因为 foreach 不是 LINQ 函数?我不明白 void 在哪里!

最佳答案

  • ForEach()类型为 void .

  • Select()返回 IEnumerable<T> , ToList()返回 List<T>

所以:

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

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

因为你不能分配 voidList<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);
}
}

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

foreach (var ip in query)
{
PAINTIP(ip);
}

关于c# - 无法将 void 分配给隐式类型的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8242805/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com