这是我的代码,效果很好:
IPRepository rep = new IPRepository();
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()
});
IP为IEnumerable<string>
PAINTIP(ip)
的调用之上添加此代码。PAINTIP(ip)
。
因此我需要使用某种foreach函数,但我无法弄清楚如何。答案 0 :(得分:2)
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()
})
.SelectMany(item => item.IP)
.ToList()
.ForEach(PAINTIP)
答案 1 :(得分:0)
非常类似于这个问题的答案 LINQ equivalent of foreach for IEnumerable<T>
IEnumerable没有For Each,但是List
items.ToList().ForEach(i => i.DoStuff());
修改强>
我不确定你能否以你想要的方式做到这一点:
ep.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()
})
.ToList().ForEach(IpObj => IpObj.IP.ToList().ForEach(ip => PAINTIP(ip));
这样您就可以先获取IpDTO对象列表。然后枚举每个,然后依次遍历每个IpDTO对象IP IEnumerable。我还没有看到一种方法,你可以在你的IpDTO对象的创建内枚举。如果某人有一个如何做的例子,我希望看到自己学习。
答案 2 :(得分:0)
您可以这样做:
var result = from item in collection
.Where(i => i.condition == value)
.Select(i => new{ Name = i.name, Description = i.Description })
.ToList().ForEach(i => Function(i));
希望这有帮助!