3

I've got a list of person objects, and I want to be able to programmatically get an anonymous type that contains a subset of that object's properties. I've got a list of Person objects like: (VS 2010 .NET 4.0). Took it for granted, but yes, the Person data is persisted in a database.

var personList = new List<Person>()
 {
      new Person(){ PersonId=11, Weight=100, Race="Green", Height=230},
      new Person(){ PersonId=22, Weight=110, Race="Blue", Height=130}
 };

and based on a user selecting which particular properties they want to see I'd like to emulate

var query = from c in personList
            select new
             {
                 Weight = c.Weight,
                 Height = c.Height,
                 PersonId = c.PersonId
             };

where in this situation the user from a user interface selected PersonId, Weight and Height as properites they wished to use to create a new anonymous type.

I've got the following code which will take a given take a print the property values to the screen of what a (simulated) user might choose:

PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(Person));
var propList = new List<string>()
{
     "PersonId","Weight","Height"
};

for (int i = 0; i < personList.Count; i++)
{
      for (int j = 0; j < props.Count; j++)
      {
          if (propList.Contains(props[j].Name))
          {
              //properties added to object here..but can't return anonymous type from method....
              Console.WriteLine(props[j].GetValue(personList[i]));
          }
       }
 }

this prints 11, 100, 230 and 22, 110, 130 to the console.

What I'm trying to do is essentially recreate the code in var query... but be able to pass a list of properties to the select new portion of the query.

wootscootinboogie
  • 8,461
  • 33
  • 112
  • 197
  • 1
    @Brandon I'm pretty sure that is not the matter here – Selman Genç Feb 07 '14 at 15:05
  • 1
    Oh come on now... It's Friday, have a laugh. – Brandon Feb 07 '14 at 15:09
  • 1
    All of the blue people I've seen usually fit this description. The proportions check out. We can now help with the code. – Dmitriy Khaykin Feb 07 '14 at 15:16
  • @DavidKhaykin these are Toydarians, they're weighed is based on a logarithmic scale. – wootscootinboogie Feb 07 '14 at 15:17
  • Can you share the signature of the theoretical method that you want to implement? – Adrian Salazar Feb 07 '14 at 15:27
  • I envision something like `GetSubTypeFromPropertyList(IEnumerable data, List propNames)` – wootscootinboogie Feb 07 '14 at 15:29
  • @wootscootinboogie And what do you expect the return type to be? Also, if your source input is an `IEnumerable`, not an `IQueryable`, then it means the query isn't translated into a query, and given that that's the case, I see no purpose to doing this. If it's just linq to objects, you may as well just keep the objects as `Person` objects. – Servy Feb 07 '14 at 15:30
  • @Servy this was for simple demonstration purposes but in reality my problem is to allow the user to select a group of objects with an ID, and left join onto the different attributes. So in reality the properties on the dynamic object aren't necessarily all from a parent `Person` object, but I assumed it would be simpler to explain the way I did. – wootscootinboogie Feb 07 '14 at 15:34
  • @Servy further, I would suspect (definitely don't know) that the return type would be void and use an output parameter to set a dynamic object to whatever the user enters in an as properties (since you can't return anonymous types from methods). Dealing with dynamics in C# is new to me, so I'm just seeing what fits. – wootscootinboogie Feb 07 '14 at 15:36
  • @wootscootinboogie My points still remain. If this is an `IEnumerable` then none of this is translated to the DB, so I don't see a point. What do you gain from copying some of the properties of an object through LINQ to objects? – Servy Feb 07 '14 at 15:42
  • @Servy Let's pretend I do a .AsQueryAble() before I set the dynamic object. Perhaps I'm not phrasing my question well enough, but the gist of it is I want to allow the user to create their own anonymous types, based on whatever LINQ queries they run, and when they are done adding properties to their anonymous type have some sort of action performed on the resultant object. – wootscootinboogie Feb 07 '14 at 15:48
  • @wootscootinboogie If you call `AsQueryable` then you're still doing everything in LINQ to objects, thus I would content there is no purpose to this method at all. Again, I clearly see what you're asking, what I don't see is why you would want to do it. What is projecting your query to one with a subset of its fields, given that it's not translated into a database query, accomplishing? – Servy Feb 07 '14 at 15:53
  • @Servy in my model a group of Person IDs is chosen by some means (say, over 50 years old). Then, with LINQ I left join attributes of that cohort on their Person ID. These are displayed as a table, where a column is a property name of an object (a row in a table = object). Then, when the user is finished, those data are sent for some processing. The projection is really the table itself, I just want to be able to process the data, and still get the property names of the dynamic object. – wootscootinboogie Feb 07 '14 at 16:00
  • @wootscootinboogie And why do you need to project out only a subset of the properties to process the data? Why can't whatever is doing the processing simply ignore those properties that it doesn't need? – Servy Feb 07 '14 at 16:04
  • @Servy I think I tried to simplify my intent too much. I simply want to be able create anonymous types, do some processing on them, and be able to retrieve the anonymous type's property names, which after the processing, with have additional information associated with them. – wootscootinboogie Feb 07 '14 at 16:14
  • @wootscootinboogie And I'm saying that there is literally no point in doing this. It would be a considerable amount of work that will accomplish nothing towards whatever goal you actually have, that you're still refusing to actually explain. – Servy Feb 07 '14 at 16:16

1 Answers1

0

Have you seen this thread?

It refers the poster to the ExpandoObject on MSDN, which allows you to create anonymous types dynamically.

Community
  • 1
  • 1
Brandon
  • 4,491
  • 6
  • 38
  • 59