TheBoubou
TheBoubou

Reputation: 19933

Select some records

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.

Upvotes: 1

Views: 160

Answers (3)

Marc Gravell
Marc Gravell

Reputation: 1063619

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

Upvotes: 1

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

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.

Upvotes: 0

Jelle
Jelle

Reputation: 322

You can do this with select (LINQ):

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

Upvotes: 2

Related Questions