Is it possible to make Dynamic LINQ work with Entity Complex Types?

c# dynamic-linq entity-framework-4


I want to dynamically query an object with System.Linq.Dynamic.

 var selectData = (from i in data 
                      select i).AsQueryable().Where("Name = @0","Bob1");//This works fine with a non-entity object

I know that we cannot project onto a mapped entity. I believe that is the reason this code fails

        foreach (var item in rawQuery.ObsDataResultList)
            var propertyData = (from i in item
                                select i).AsQueryable().Where("PropertyName = @0", "blah");
        }//item is a Entity Complex Type


Could not find an implementation of the query pattern for source type 'ClassLibrary1.Model.bhcs_ObsData_2_Result'. 'Select' not found.

Given the fact that I need to specify the PropertyName at runtime, I don't see any way to project with an anonymous type or a DTO.

I don't need to retain any of the Entity functionality at this point, I just need the data. Copying the data onto something that is queryable is a valid solution. So, is it possible to query entity framework with dynamic LINQ?

And here is the entity class header (the thing I'm trying to query, aka the item object)

[EdmComplexTypeAttribute(NamespaceName="MyDbModel", Name="blah_myQuery_2_Result")]
public partial class blah_myQuery_2_Result : ComplexObject
5/23/2017 12:11:25 PM

Accepted Answer

First of all, let me clarify that System.Linq.Dynamic is not a full fledged Microsoft product. It is just a sample we release some time ago, and we don't thoroughly test different LINQ implementations to work correctly with it. If you are looking for a fully supported text-based query language for EF ObjectContext API you should take a look at Entity SQL instead.

Besides that, if you want to use System.Linq.Dynamic and you are ok with testing yourself that you don't hit anything that will block your application from working, then I'll try to see if I can help. I am going to need additional information since I am not sure I understand everything in your code snippets.

First of all I would like to understand, in your first example what is "data" and where did it come from? In your second snippet, what is "rawQuery" and where did it come from? Besdies, what is rawQuery.DataResultList and what is rawQuery.ObsDataResultList?

Also regarding your second snippet, it seems that you are trying to compose with query operators on top of an object that is not actually of a query type (although that doesn't explain the error you are getting given that you are calling AsQueryable the compiler should have complained before that bhcs_ObsData_2_Result is not an IEnumerable nor a non-generic IEnumerable).

In your propposed answer you are saying that you tried with ObjectResult and that seemed to help. Just be aware that ObjectResult is not a query object and therefore it won't allow you to build queries that get send to the server. In other words, any query operators that you apply to ObjectResult will be evaluated in memory and if you don't keep this in mind you may end up bringing all the data from that table into memory before you apply any filtering.

3/26/2012 10:56:46 PM

Popular Answer

Query ObjectResult<blah_myQuery_2_Result> directly instead of the item blah_myQuery_2_Result. For example

var result = (from i in rawQuery.DataResultList
                          select i).AsQueryable().Where("CreatedDTM > @0", DateTime.Now.Subtract(new TimeSpan(30, 0, 0, 0)));

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow