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
因为你不能分配 void
至 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);
}
}
或
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/