2

This is my code that works great:

  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 is IEnumerable<string>

  1. I need to add to this code above a call to a function PAINTIP(ip).
  2. I need to send each one of the elements that will be inside IP to a function PAINTIP(ip). therefore i need to use some kind of foreach function but i cannot figure out how.
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Tania1990
  • 183
  • 1
  • 1
  • 7

3 Answers3

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)
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • i need to call the PAINTIP function only with the IP i have the IP inside the select : IP = z.Select(x => x.IP).Distinct() – Tania1990 Nov 23 '11 at 11:13
  • IP can give lost of ip numbers, Function PAINTIP recives only one ip and paint its history. therefore i need to take each one of the ip numbers that will be recieved by the IP and call the function PAINTIP with each one of the ip numbers – Tania1990 Nov 23 '11 at 11:20
  • dont i need to put something inside the PAINTIP(?) i mean dont i need to PAINTIP(item) ? – Tania1990 Nov 23 '11 at 11:27
  • You don't have to since you are sending elements of that sequence. Try and see if it works – Ufuk Hacıoğulları Nov 23 '11 at 11:29
  • what type will be item ? string? – Tania1990 Nov 23 '11 at 11:55
0

Very similiar to the answer of this question LINQ equivalent of foreach for IEnumerable<T>

There is no For Each for an IEnumerable but there is for List

items.ToList().ForEach(i => i.DoStuff());

Edit

I'm not sure you can do it in the way you want to other than this:

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

This way you get your list of IpDTO objects out first. Then Enumerate over each and then in turn over each IpDTO objects IP IEnumerable. I haven't seen a way to do it where you can Enumerate inside the creation of your IpDTO object. If someone has an example of how to do so I'd like to see to learn myself.

Community
  • 1
  • 1
Chris
  • 3,114
  • 1
  • 18
  • 27
  • i saw this quesion yesterday but i am having trouble with doing the same inside my code therefor i decided to ask this question, im not sure where to put this list foreach inside my code and how { IP.ToList().ForEach(i => i.PAINTIP(x)); }); – Tania1990 Nov 23 '11 at 10:59
  • Which part of your code are you looking to enumerate over? Q will be an IEnumerable of type IpDTO and doing Q.ToList().ForEach(i => i.PAINTIP(i)); Will do a foreach on the results. Is that not what you're after? – Chris Nov 23 '11 at 11:03
  • i need to call the PAINTIP function only with the IP i have the IP inside the select : IP = z.Select(x => x.IP).Distinct() – Tania1990 Nov 23 '11 at 11:11
  • IP can give lost of ip numbers, Function PAINTIP recives only one ip and paint its history. therefore i need to take each one of the ip numbers that will be recieved by the IP and call the function PAINTIP with each one of the ip number – Tania1990 Nov 23 '11 at 11:20
  • Edited my answer In short it performs the GetIp() method first, then chains on the end a way to enumerate over each IP of each IPDTO object. – Chris Nov 23 '11 at 11:21
  • each one of the ip that will be called by the PAINTIP function will be strings right ? – Tania1990 Nov 23 '11 at 11:40
0

You can do something like this :

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

Hope this help !

Krypto_47
  • 141
  • 1
  • 1
  • 6
  • i think i understand but from inside the select i need only the IP I dont need all the other objects like IId. How can i ForEach only the IP? – Tania1990 Nov 23 '11 at 11:04