.NET dynamic Linq

I am writing a post following a question from @Silas around the dynamic.cs file that is present in the generative objects Framework, wondering if this is a re-implementation of something that exist in .NET Framework. @Garth this is a topic that might be of interest to you too, for its impact it can have for the migration to .NET Core and also some discussions we already started around GraphQL.

This is an interesting topic, as there could be other design options to achieve what is done with dynamic Linq queries in generative objects and is a question also related to the APIs, potential use of GraphQL etc.

The need for dynamic queries comes from the fact that we had the requierement to make api calls to query data with filter predicate expressions, for example “give me all contracts for customer X with date after july 2019”. These queries needs to be expressed in text format, as part of the http request. And server side, these text queries need to be interpreted to actually query the data.

The solution I choose at that time was to follow the Linq syntax, and then, server side, apply these Linq queries, since Linq is the mechanism we use server side to query data.

The problem was that it is not straightforward to convert text Linq queries to actual Linq queries. For example, if the query text for a query over contracts is :

“Customer.Name == \“MyCustomer\” && Date > \“2019-06-20\””

The actual Linq query should be :

contracts.Where(c => c.Customer.Name == “MyCustomer” && Date > new DateTime(2019,6,20))

The dynamic.cs class that I added is to actually allow to do these text queries over Linq and allow this syntax:

contracts.Where(“Customer.Name == \“MyCustomer\” && Date > \“2019-06-20\””)

In my understanding, this support does not exist by default in .NET Framework, but I can be wrong because I never check if such support was added in most recent version of .NET !

Here is where I found the initial implementation of dynamic.cs :

https://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

And here is, from my old blog, the explanation of an addition I made to the original dynamic.cs extention :

Now the questions are :

  • Is there direct support for dynamic Linq queries in .NET Framework, and in .NET Core ? And does it support the Contains keyword I added ?
  • Are there other options for supporting dynamic queries ? especially if we move to GraphQL APIs, is there any suggested mechanism for queries ?

Hi Walter,

I did a quick search for dynamic linq for .net core and found this, which is actively developed:
https://www.nuget.org/packages/System.Linq.Dynamic.Core/ - is this equivalent?
Docs site is here: https://dynamic-linq.net/.

If the use case is query expressions that support where clauses and joins then certainly GraphQL is a candidate query syntax, as is OData which provides a standardised way to query REST APIs:
https://www.odata.org/
https://www.nuget.org/packages/Microsoft.AspNetCore.OData/

While I’m here, there are a number of very interesting looking tools for EF Core here:
https://docs.microsoft.com/en-us/ef/core/extensions/
Have a read through - a couple of designers worth trialling, and some interesting extensions as well.

Cheers

1 Like

Using EFCore might be nice. But. As I encountered some issues with this in the past : You can’t choose if the produced output will be targetting the same table or multiple. (Target per Entity vs Target per Entity Hierarchy.)

I think, this feature disappeared with EF6, and was not implemented in EFCore.

1 Like

Oh yes this is exactly it ! so they actually created a project for this, this is nice, they even integrated the Contains keyword that I did integrate on the initial dynamic Linq implementation from Scott team. I will integrate this version at some point (don’t think it is necessary to do it now). The lambda expression parsing can also be pretty useful !

Yes definitely something to explore at some point

We are not using entity framework (EF). The choice was to go to NHibernate because there is a java version of it, for in case we would want to migrate one day to java. The idea here is to not use any big Microsoft specific framework but rather pure C#, to make it easier for potential port to other technologies.

By the way, I foresee removing NHibernate itself, as we have all the knowledge on the application metamodel to generate our custom, highly optimized SQL query code, not depending on any ORM

@Silas , as explained in my answer to Garth : there is no plan to use EFCore at the moment