1

I am trying to create a some kind of dynamic loader for my models where I can specify which properties I need, the main purpose of it is to create a REST API which provides dynamic information as JSON for individual models.

The API would be accessed by e.g. /api/model?id=581D1393-3436-4146-A397-E47CF5419453&fields=foo,bar,baz

For this purpose I used the Dynamic LINQ like described in ScottGu's Blog, the problem is that I need to do queries over multiple tables with joins and load data from different tables, which I can't do in this case, as far I know.

Now I use ObjectQuery<DbDataRecord> approach with Entity SQL where I can create a query however I want, but in this case I lose compiler validation and it is harder to refactor.

My question is, is there a there a best practice scenario for this kind of problem? And is it may be simpler to achieve with some other ORM?

Greetings

Russlan A.

aruss
  • 354
  • 1
  • 13
  • You can do the 2nd step based on the 1st step, which means you can extract the common query and store the data in the memory (use .ToList() or the similar) and then based on what you have you just need simple if..else or switch to finish the rest logic. Dynamic Linq is over killing. – zs2020 Sep 22 '11 at 22:13

1 Answers1

0

Actually I think you can do it, though you think you can't. I am saying this because of the laze loading.

using(var someContext = new SomeContextEntities())
{
    var data = someContext.Entity;

    foreach(var kvp in queries)
    {
        switch(kvp.Key)
        {
            case "q1":
                var val = kvp[kvp.Key];
                data = data.Where(q => q.q1 == val);
                break;
            case "q2":
                var val2 = kvp[kvp.Key];
                data = data.Where(q => q.q2 == val2);
                break;
            case "q3":
                var val3 = kvp[kvp.Key];
                data = data.Where(q => q.q1.q3 == val3);
            ...
        }
    }
    return data.ToList();
}

If you look at the generated script or result set, you will get what you want.