0

What are the possible ways to create a LINQ expression dynamically, but using the query syntax? Is the query syntax a C# thing only, and if so, is the only viable way of creating such expressions using Roslyn dynamic compilation?

When writing LINQ expressions manually, I find them more natural when written using method chaining syntax, for example ctx.Foo.Where(foo => foo.Type.Name == "Bar") but there are some cases where I would need to write them like this:

from foo in ctx.Foo
join fooType in ctx.Types on foo.TypeId equals fooType.Id
where fooType.Name == "Bar"

I love how expression trees ensure type safety when creating expressions dynamically, but how would one create expressions using the query syntax?

Dejan Janjušević
  • 3,181
  • 4
  • 41
  • 67
  • Hi,I believe that your question is simple but I cannot understand what you want. I advise you to simplify the things to be able help you. you can say I need 1 2 3 with example. – Mohammad Ghanem Jul 04 '19 at 12:19
  • @MohammadAlghanem thanks for the comment. However I'm not sure how to simplify the question even more, it's literally about whether it's possible or not to create LINQ query syntax expressions. EDIT: see https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/query-syntax-and-method-syntax-in-linq for query syntax vs. method syntax – Dejan Janjušević Jul 04 '19 at 12:26
  • Do you want to create Expression class objects? https://learn.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression?view=netframework-4.8 Can you please specify what "expression" is a bit more? – iSpain17 Jul 04 '19 at 12:55
  • I don't think it is possible. You can create an expression that will return for you an `IQueryable` and after that you can call methods to change it (Where, ORderBy, GroupBy, etc..). I enjoy the predicate builder class that allows you to easly work with expression trees. – Felipe Oriani Jul 04 '19 at 13:01
  • I think a reverse-engineer path from method syntax to query syntax isn't possible, because method syntax is a great deal richer. I.e. all query syntax can be compiled as an expression tree, but not all expression trees have a query syntax equivalent. – Gert Arnold Jul 04 '19 at 13:10
  • Query syntax is just a language sugar and compiler will convert it to method chaining syntax. There is no such things like `from .. in .. where` on the CLR level – Aleks Andreev Jul 04 '19 at 13:11

1 Answers1

0

Thanks everyone for your comments. So it turns out it's not possible to do this because query syntax is just a C# language syntactic sugar.

Additionally, if someone else stumbles upon this question, take a look at the excellent answer by @Gert: https://stackoverflow.com/a/15599143/828023

That answer explains that the query syntax is "sugar", while the method syntax shows what really goes on under the hood, where for example join x in y on z equals x.something into somethingElse is actually a GroupJoin method call and there is no way to express this with expression trees without actually calling GroupJoin.

Dejan Janjušević
  • 3,181
  • 4
  • 41
  • 67