0

Good day everyone,

a property or a field name of a table is being stored in another table field [I know bad database design practice], so what I'm trying to do is converting the string into a variable using GetType().GetProperty("some string goes here") technique in the select statement like this Select(m => m.GetType().GetProperty(x.data)), though, everytime I try to execute the statement an error is thrown:

Argument type 'System.Collections.Generic.List`1[System.Reflection.PropertyInfo]' does not match the corresponding member type 'System.Linq.IQueryable`1[System.Reflection.PropertyInfo]' (Parameter 'arguments[3]')

User
  • 123
  • 4
  • 13
  • Do all fields have the same type? – vernou Jun 11 '21 at 13:49
  • Yeah, all fields are string – User Jun 11 '21 at 13:50
  • `know bad database design practice` don't do this then. ORMs are meant to map objects to relational tables anyway, and that design is *not* a table. On the Object side, you're supposed to have actual objects, not tables. Trying to fix the schema at the *query* is exactly the wrong place to do this. The problem is in the *mapping* logic, not the query – Panagiotis Kanavos Jun 11 '21 at 13:56
  • @PanagiotisKanavos, it's not up to me, I'm a mere developer, not the project leader, so I can't express my opinion, all I can do is execute the orders. – User Jun 11 '21 at 14:03
  • That doesn't mean what you want is possible, or that it's any better than rewriting everything. Unless you misunderstood the request. Inheritance in ORMs is often handled by using different tables per type, either holding all fields (Table-per-Class), or holding just the extra fields (Table-per-Type). Using a field value in one field to look up a field in another table simply isn't supported by ORMs. – Panagiotis Kanavos Jun 11 '21 at 14:08
  • In any case, you need to post the actual classes and tables. Trying to perform the mapping at the query level is simply wrong. The query needs to be translated to SQL and the code you tried can't be translated at all – Panagiotis Kanavos Jun 11 '21 at 14:09
  • I don't think it's possible to do think in EF Core query. Maybe you can do this after the query execution like `context.MyTable.ToList().Select(...)`. – vernou Jun 11 '21 at 14:17
  • Does this answer your question? [LINQ : Dynamic select](https://stackoverflow.com/questions/16516971/linq-dynamic-select) – Munzer Jun 11 '21 at 15:06

0 Answers0