tags:

views:

102

answers:

2

Hey, I'm very new to linq and lamba expressions. I'm trying to walk a collection, and when I find an item that meets some criteria I'd like to add that to another separate collection.

My linq to walk the collection looks like this (this works fine):

From i as MyCustomItem In MyCustomItemCollection Where i.Type = "SomeType" Select i

I need each of the select items to then be added to a ListItemCollection, I know I can assign that linq query to a variable, and then do a for each loop adding a new ListItem to the collection, but I'm trying o find a way to add each item to the new ListItemcollection while walking, not a second loop.

Thanks ~P

+1  A: 
        ListItemCollection   lc = new ListItemCollection();
        lc.AddRange(
          (
            from i in MyCustomItemCollection 
              i.Type = "SomeType" 
            select new ListItem(){
               //Construct item here
            }
          ).ToArray()
        );
Nix
Thanks, very helpful
Prescott
A: 
    var MyItems = (From i as MyCustomItem In MyCustomItemCollection 
                   Where i.Type = "SomeType" 
                   Select i).ToArray();
ListItemcollection MyListItemcollection = new ListItemcollection();
MyListItemcollection.AddRange(MyItems);
Ben Robinson