0

I'm looking to create a method that does something like ConvertFieldNamesToSelect below, which would dynamically create the select expression from a string array of column names, but I'm not sure if it's possible?

using (var dc = new MyDataContext(myConnectionStr))
{
    IQueryable<User> query = dc.Users.Where(u => u.UserID == 4);
    List<User> users = query.Select(ConvertFieldNamesToSelect<User>(new string[] { "UserID", "Name" })).ToList();
    return users;
}

I've seen threads like: LINQ : Dynamic select

Unfortunately they don't work correctly with Linq To Sql. At database level they either select all fields then refine afterwards (inefficent, visible via profiler) or throw an exception Explicit construction of entity type 'X' in query is not allowed because they are attempting to create an e.g. User() object in the query (rather than a dynamic object), which Linq To Sql doesn't allow, but regular Linq on lists does.

dc.ExecuteQuery(sql) could of course select the fields via a SQL statement, but that would mean IQueryable benefits are lost, and we have lots of existing code building those up dynamically. Would also consider DynamicLinq if that's an option.

mhapps
  • 1,023
  • 8
  • 15
  • I would suggest you look into [Dynamic LINQ](https://github.com/StefH/System.Linq.Dynamic.Core), just be careful of user inputs. Otherwise you would need to create your own code to create objects (e.g. consider: LINQ to SQL expects an object from the database like `User` to have all fields matching the database. You can't create a partial `User` object and expect it to work properly.) – NetMage Dec 13 '21 at 22:19
  • @NetMage As touched upon I'm not entirely sure it is possible in Dynamic LINQ or by creating a custom Expression Tree (which I'd imagine DLinq is wrapping), since the problem is expressions can't create dynamic/anonymous types, which are worked out at compile time and Linq To SQL requires. It may be that I need to give up with strings arrays for field names and pass through TEntity expressions with dynamic object funcs higher up the chain. – mhapps Dec 13 '21 at 22:36
  • Dynamic LINQ includes the code to create anonymous classes/types at runtime (I have my own cobbled together module to do it as well, though I cheat a bit). The issue is your result is going to be `List` because you don't know the type at compile time. – NetMage Dec 13 '21 at 23:30
  • Thanks @NetMage, that sounds encouraging, will dig in. List should be fine and castable back to TEntity. – mhapps Dec 14 '21 at 09:02
  • @NetMage ah, I'm assuming you just reflect the properties out of the temporary anonymous type via DynamicClass in Dynamic LINQ? Seems to work e.g. query.Select("new (UserID, Name)") – mhapps Dec 14 '21 at 12:06
  • Normally in a dynamic situation you would use the result in a context such as a `GridView` which will handle the reflection for you. It really depends on your final use of such a dynamic object - e.g. I would often consider using a `DataTable` or `Dictionary` instead of Reflection if I needed to process the fields a lot. – NetMage Dec 16 '21 at 20:00

0 Answers0