0

If you want to create an anonymous object from another in c#, you can use the handy Linq method select:

var myList = new List<MyClass>();

myList.Add(new MyClass(valueA, valueB, valueC));
myList.Add(new MyClass(valueA, valueB, valueC));

var myResult = myList.select(l => new { l.PropA, l.PropB });

I wonder, if there is a built-in method, some class or a NuGet package that allows you to do the same from a list of strings. I imagine something like:

myResult = myList.select("PropA", "PropB");
// or
myResult = myList.select(myPropertyEnumerableOfString);

Is there something built-in, or do I need to iterate over my property list using reflections?

André Reichelt
  • 1,484
  • 18
  • 52
  • Do you want a dynamic object? – vernou Mar 13 '20 at 13:10
  • 2
    no, you can not create anonymous objects like that since the .net still needs to know the explicit type, wich it does in your sample, since the compiler will pick it up. however you could just use a `Dictionary` like `Dictionary` – Patrick Beynio Mar 13 '20 at 13:13
  • 1
    Sounds like an XY problem. What are you planning to do with `myResult`? – Sweeper Mar 13 '20 at 13:14
  • @Orwel It would be fine. In a web-api I want the user to pre-group the data by a list of properties. I already found and implemented the following solution for grouping, but that one returns my whole object as key. I want to filter out the relevant properties before delivering the data to the client. https://codereview.stackexchange.com/questions/173128/groupbyparams-string-fields – André Reichelt Mar 13 '20 at 13:14
  • @Sweeper See my answer above. – André Reichelt Mar 13 '20 at 13:14
  • 1
    The value of the string is not known at compile time, so...no. How could it work (at compile time)? This could only work at run-time (i.e. with reflection). – Wyck Mar 13 '20 at 13:17
  • The library AutoMapper can generate Object of typeA from Ojbect of TypeB and copy properties with same name. Maybe it can generate an anonymous class on runtime or dynamic object – vernou Mar 13 '20 at 13:49
  • @AndréReichelt An anonymous object, ist just a class-name that is generated by the compiler for you at compile-time. You save to create a class, give it a name etc. There is nothing done at runtime. This is what might confuse people in your approach. If you do not know the data you are processing, anonymous types are not applicable. Or in other words.If you can create a class manually for your strings - than and only than - you might also use the shorthand way of an anonymous type. You might mixup type inference, what you know and feel from intellisense, with things happening at runtime. – Holger Mar 13 '20 at 13:57
  • Does this answer your question? [LINQ : Dynamic select](https://stackoverflow.com/questions/16516971/linq-dynamic-select) – Selim Yildiz Mar 13 '20 at 13:57
  • @Holger It's possible to generate and add class in running AppDomain. That do Mock libraries. – vernou Mar 13 '20 at 16:35
  • @Orwel. Sure, many things is possible. Just anonymous classes are the wrong keyword for that. That's a syntactical sugar and only exists at compile time. Same as using "var" for implicitly typed variables. An anonymous class is an anonymous named and compile time generated class. So it's like asking for how to create a "var" at runtime - a question that just doesn't make any sense. – Holger Mar 14 '20 at 09:14
  • @Holger Then let me precise my question: Is it possible to generate a dynamic JSON object using System.Text.Json? – André Reichelt Mar 16 '20 at 07:55
  • @AndréReichelt Json is Plain Text, any Reader/writer/Serializer technique is object to text-mapping. If you have some objects you can use libraries to create the json-text. This does not mean, you are required to create objects first, to create json-text. You can create the text from any source you want, from database tables, any possible input, without converting it to objects first. I haven't understood what your original data are. But most likely you can create text directly, or a Jsondocument, just by adding json-element by jason-element. Without the need to convert object properties. – Holger Mar 16 '20 at 12:09

0 Answers0