开发者

Select some records

开发者 https://www.devze.com 2022-12-27 15:51 出处:网络
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>.

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.


You can do this with select (LINQ):

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号