0

I want to write a selector using System.Linq.Expressions and pass it as an argument to custom Find(selector) method, but which columns should be grabbed is decided by a list of strings, where every string is a required column name.

    public Task<List<object>> ExportFromServer(IEnumerable<string> columnNames)
    {
      // it is a typical solution, how to use columns string name here?
      Expression<Func<DirectoryCountry, object>> selector = country => new { country.Code, country.CountryCode }; 

      return Find(selector);
    }
  • How is EF related to this code? Even if `Find` referred to `DbSet.Find` it would return a single T, not an untyped list. – Panagiotis Kanavos Aug 27 '20 at 13:48
  • Sorry, it is not related. Edited. – Roman Larrsen Aug 27 '20 at 13:49
  • 2
    What is the actual problem this code tries to solve? I suspect it would be a *lot* easier to solve it in a way that doesn't involve `object`, `List` or runtime expressions – Panagiotis Kanavos Aug 27 '20 at 13:49
  • Older classes line Column Names do not have linq enumerations. So for a datatable use something like this : string[] names = dt.Columns.Cast().Select(x => x.ColumnName).ToArray(); – jdweng Aug 27 '20 at 13:51
  • loosely related: https://stackoverflow.com/questions/821365/how-to-convert-a-string-to-its-equivalent-linq-expression-tree – Daniel A. White Aug 27 '20 at 13:52
  • @jdweng how did you guess this is about DataTable? If it is, DataTable offers ways to select columns dynamically, there's no need for this code – Panagiotis Kanavos Aug 27 '20 at 13:52
  • essentially you will be responsible for building the expression tree. is this ef core? – Daniel A. White Aug 27 '20 at 13:52
  • I want to allow a user to download table data from a database and make it available to choose which columns should be downloaded. Don't pay attention to Find method, the main problem here is how to dynamically build Expression using columnNames data – Roman Larrsen Aug 27 '20 at 13:53
  • 2
    @RomanLarrsen what problem is this trying to solve? Whatever it is, there are probably far easier ways to do it. ORMs like EF are meant to produce *strongly-typed objects*. If you don't want that, it's better to go back to ADO.NET. Unless you want to control the generation of the SQL query? – Panagiotis Kanavos Aug 27 '20 at 13:53
  • 1
    Emulating `new {...}` dynamically, i.e. introducing a new anonymous type at a runtime, is already a hard thing ( [like here](https://stackoverflow.com/a/59315794/1075282) ). This is not something you need to opt for columns in table for downloading – Renat Aug 27 '20 at 14:33
  • @Panagiotis Kanavos : OP said Columns and ColumnNames – jdweng Aug 27 '20 at 15:57
  • 1
    I think this may be an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What do you mean by "downloaded"? What exact problem are you trying to solve? Also, [Dynamic LINQ](https://github.com/StefH/System.Linq.Dynamic.Core) may be helpful - it contains code for creating anonymous types on the fly and string based querying. – NetMage Aug 27 '20 at 20:33
  • @jdweng That is just db talk - the sample selector shows a POCO type which implies not datatables. – NetMage Aug 27 '20 at 20:34
  • Thanks, @NetMage, you are a lifesaver, I absolutely forgot about the dynamic LINQ. Btw, it is not an XY problem, I know the actual problem, but due to the current obstacles on my project and upcoming deadlines, this solution is the only one which helps solve problem asap – Roman Larrsen Aug 28 '20 at 11:13

0 Answers0