1

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

Error

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")]
[DataContractAttribute(IsReference=true)]
[Serializable()]
public partial class blah_myQuery_2_Result : ComplexObject
{
Community
  • 1
  • 1
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • I'm following this with interest as I haven't learned much about dynamic linq yet. But I have to say (purely subjectively, as a matter of style) that I find it confusing to mix query comprehensions with method calls in the same expression, especially if the query comprehension is the trivial `from i in data select i`. I think you'll find the code much more maintainable if you change `(from i in data select i).AsQueryable()` to `data.AsQueryable()`. – phoog Mar 21 '12 at 21:16

2 Answers2

2

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.

divega
  • 6,320
  • 1
  • 31
  • 31
  • `data` represents a mock object query. `rawQuery` is an object instance of custom class. The class has 4 members of type `List` (`DataResultList`, `ObsDataResultList`). It came from a read operation in the DL. The DL queries a set of stored procedures based on some parameters. The params perform the filtering before the example code. So, I'm not worried about loading the full table in memory, aka deferred execution for this case. Although, that is a good point to consider in general. – P.Brian.Mackey Apr 18 '12 at 14:13
0

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)));
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348