views:

44

answers:

3

I have an IList<MyList>. I'd like with LINQ keep the same list (same number of record) but I'd like reduce or/and rename some record. At the end I'd like to have IList<MyNewList>.

Update (Marc Gravell request) We have tools to generate interface/object from Oracle stored procedure. My problem is, for some stored procedure, a lot of field are created, normal it's returned by the database (and change the database is not an option). Then the tools generated "Field1,Field2,Field3,Field4,..." but I'd like create a new list with only "Field2,Field4". this new list will be binded with a GridView.

+2  A: 

You can do this with select (LINQ):

var newList = list.Select(x => TranformToMyNewList(x)).ToList();
Jelle
A: 

In linq you can create Anonymous Type: http://msdn.microsoft.com/en-us/library/bb397696.aspx. Its usage is something like this:

var anonymousTypeList = (from row in list select new {Name=row.FullName,Address=StreetAddress}).ToList();

Regards.

Shoaib Shaikh
+1  A: 
var newList = (from item in oldList
               select new { item.Field2, item.Field4}).ToList();

or for your own type:

IList<MyNewList> newList = (
       from item in oldList
       select new MyNewList {Field2=item.Field2,Field4=item.Field4}).ToList();
Marc Gravell
In fact I need to select some record from the initial list ...
Kris-I
@Kris - then add a `where`, and perhaps use `Single()` instead of `ToList()`
Marc Gravell