Make an anonymous object for the Linq Select statement

I get an array of strings that have property names. I only want to load these properties when retrieving data, in this case from the Entity Framework database. Sort of:

var result = db.myTable
               .Where(x => x.Id == "someValue")
               .Select(y => new {y.someProperty, y.someOtherproperty, ...});

How to create an anonymous object from an array of strings. I would like to have something like:

var MyObj = new {};
foreach(var I in MyStrinArr)
{
   ... Add the properties here ...
}
var result = db.myTable.Where(x => x.Id=="someValue").Select(y => obj);
+4
source share
1 answer

This is not possible (directly), since anonymous classes are generated at compile time. Iterating over an array of strings is done at runtime. So this is not comparable.


linq, ScottGu   : dynamic-linq . dynamiclinqcsharp . DynamicLibrary.cs ClassFactory, . public Type GetDynamicClass(IEnumerable<DynamicProperty> properties)

+3

Source: https://habr.com/ru/post/1660082/


All Articles