tags:

views:

143

answers:

4

Hi all,

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.

+8  A: 

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();
LukeH
+4  A: 

if you are on .NET 3.5 or greater this could be done by using LINQ projections:

basicObjectList = extendedObjectList.Select(x => new BasicObject { Id=x.Id, Name=x.Name})
KJN
+2  A: 
var basicObjectList = extendedObjectList.Select(eo => new BasicObject { name = eo.name, id = eo.id });
Danny Chen
A: 

I think that the OP's suggestion of "BasicObject" was just a pseudonym for a resulting object with a specific subset of properties from the original set. Anonymous types are your friend (as indicated by @mumtaz).

Assuming the following extendedObjectList if of IEnumerable<T> (including a List):

// "var" used so that runtime infers the type automatically
var subset = extendedObjectList
   // you can use any Linq based clause for filtering
   .Where(a => <conditions>)
   // where the runtime creates a runtime anonymous type to describe your "BasicObject"
   .Select(a => new { a.Property1, a.Property2, a.Property3 })
   // conversion to a List collection of your anonymous type
   .ToList();

At this point, subset contains a List of an anonymous (runtime) type that contains three properties - Property1, Property2, Property3.

You can manipulate this resulting list as follows:

// execute an anonymous delegate (method) for each of the new anonymous objects
subset.ForEach
(
   basicObject =>
   {
      Console.WriteLine("Property1 - {0}", basicObject.Property1);
      Console.WriteLine("Property2 - {0}", basicObject.Property2);
      Console.WriteLine("Property3 - {0}", basicObject.Property3);
   }
);

// grab the first object off the list
var firstBasicObject = subset.First();

// sort the anonymously typed list
var sortedSubset = subset.OrderBy(a => a.Property1).ToList();

Once the runtime has resolved the new object (of any combination of properties from the source object), you can use it virtually any way that you wish.

For Linq-to-Sql applications (using IQueryable<T>), the Select statement can be used to obtain specific column data (instead of the entire row), thereby creating an anonymous type to describe a subset of column data for a given row.

Michael