3

I have a database where I have a table that will have columns added/removed by the customer (We can't change this behavior, it's legacy).

This table has a foreign key to a table that I've mapped through EF. I'm trying to figure out a way to sort/filter the parent table by one of the dynamic columns using EF.

Is there anyway to add to the query for a column that doesn't exist until runtime?

My current solution is to get all the records, then get the corresponding records in the other (dynamic)table as a dataset, and combine the two in a new class, and sort a list of those objects.

Unfortunately, I have no code to show for this as my already working solution isn't what I want, but I have no idea where to start with a better, optimal solution.

Smeegs
  • 9,151
  • 5
  • 42
  • 78
  • You can query anything check this post http://stackoverflow.com/questions/915329/is-it-possible-to-run-native-sql-with-entity-framework – Alex Nov 13 '14 at 19:40
  • Thanks, but I'm looking for a solution that will leverage all the parts of an ORM. We're trying to move away from generating raw sql manually. – Smeegs Nov 13 '14 at 19:48
  • 1
    Have you tried using Expression Trees? – Corey Adler Nov 13 '14 at 20:03
  • @IronMan84, no I haven't. I'll look into it. – Smeegs Nov 13 '14 at 20:32
  • 1
    Look at something like this: http://msdn.microsoft.com/en-us/library/bb882637.aspx – Corey Adler Nov 13 '14 at 20:35
  • I also wonder if you could dynamically generate/emit an object model. I 'think' this is where this article is going http://blogs.msdn.com/b/mazhou/archive/2011/09/21/use-dynamic-type-in-entity-framework-4-1-sqlquery-method.aspx – thewyzard Nov 13 '14 at 21:09
  • So the "changing" table is not part of the EF model ? – phil soady Nov 13 '14 at 21:34
  • @philsoady, yes and no. It's mapped with just one column, that column is static and won't change. All the other columns are dynamic and can be added/removed – Smeegs Nov 14 '14 at 13:37

1 Answers1

0

Without some example how or what you wan't we can't help much. If you wan't to work with queries runtime, then linq is a good solution:

http://www.c-sharpcorner.com/UploadFile/deveshomar/dynamic-linq-library-in-C-Sharp/

Kobor42
  • 5,129
  • 1
  • 17
  • 22
  • Unfortunately dynamic linq doesn't work with columns that aren't mapped to a member at design time. – Smeegs Dec 18 '14 at 15:13