LINQ IEnumerable <string> </string>中的语法

时间:2011-11-23 10:50:15

标签: c# .net linq

这是我的代码,效果很好:

  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>

  1. 我需要在函数PAINTIP(ip)的调用之上添加此代码。
  2. 我需要将IP内部的每个元素发送到函数PAINTIP(ip)。 因此我需要使用某种foreach函数,但我无法弄清楚如何。

3 个答案:

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

希望这有帮助!