Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Lists: How to copy elements from one list to another, but only certain properties

Tags:

c#

list

So I have a list of objects with a number of properties. Among these properties are name and id. Let's call this object ExtendedObject. I've also declared a new List of different objects that have only the properties name and id. Let's call this object BasicObject.

What I'd like to do is convert or copy (for lack of better words) the List of ExtendedObject objects to a list of BasicObject objects. I know C# Lists have a lot of interesting methods that can be useful, so I wondered if there were an easy way to say something to the effect of:

basicObjectList = extendedObjectList.SomeListMethod<BasicObject>(some condition here);

But I realize it may end up looking nothing like that. I also realize that I could just loop through the list of ExtendedObjects, create a new BasicObject from each ExtendedObject's name and id, and push it onto a list of BasicObjects. But I was hoping for something a little more elegant than that.

Does anyone have any ideas? Thanks very much.

like image 413
MegaMatt Avatar asked Sep 28 '10 13:09

MegaMatt


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

It depends on exactly how you'd construct your BasicObject from an ExtendedObject, but you could probably use the ConvertAll method:

List<BasicObject> basicObjectList =
    extendedObjectList.ConvertAll(x => new BasicObject
                                           {
                                               id = x.id,
                                               name = x.name
                                           });

Or, if you prefer, you could use the LINQ Select method and then convert back to a list:

List<BasicObject> basicObjectList =
    extendedObjectList.Select(x => new BasicObject
                                       {
                                           id = x.id,
                                           name = x.name
                                       }).ToList();
like image 179
LukeH Avatar answered Oct 21 '22 08:10

LukeH