8

a few days back i was trying the new ORM for delphi from Devart called EntityDAC, well i was reading the docs specific the LINQ part, when i saw something like:

Linq.From(Emp).Where(Emp['Sal'] > 1000)

got to say that wake me up the first moment i saw. the expression "Emp['Sal'] > 1000" isn't a lambda expression?! since the trial version is this component don't come with sources i couldn't figure out how Where function/procedure is declared.

reference: http://www.devart.com/entitydac/docs/ -> Linq Queries -> Linq Syntax -> Scroll down to Where session

menjaraz
  • 7,551
  • 4
  • 41
  • 81
kabstergo
  • 761
  • 4
  • 16
  • 1
    I don't think that can be Pascal because the `Emp['Sal'] > 1000` expression will be evaluated before calling `Where`. Why don't you ask Devart? – David Heffernan Jan 13 '15 at 21:18
  • 2
    EntityDAC is available for .net and delphi and for sure especially the linq part is different. But there is also a chapter [Specifying LINQ Query Arguments as String](http://www.devart.com/entitydac/docs/specify_linq_query_arguments_as_string.htm) and I guess you have to use that with delphi – Sir Rufo Jan 13 '15 at 21:22
  • 1
    @DavidHeffernan What about operator overloading? Couldn't you accomplish something like by building up a list of values in the operator overload code and then using that list in the where? You effectively return an unused result. – Graymatter Jan 13 '15 at 21:25
  • 1
    @Graymatter Yes I suppose so. As Mason outlines. – David Heffernan Jan 13 '15 at 21:35
  • As soon as some people have some kind of query and an expression they starting to call that LINQ (maybe marketing?). LINQ is a .NET technology and even though it might not be copyrighted there are no other languages (except other .NET targeting languages) that have LINQ. They might have some similar or different API for streaming enumerables but it's still something different. – Stefan Glienke Jan 13 '15 at 21:42
  • 1
    @StefanGlienke: [I suppose you would know...](https://bitbucket.org/sglienke/dsharp/src/ad7c5983505f0117f1347f92d2bb96c07bdfda94/Source/Core/DSharp.Linq.QueryProvider.pas?at=master) ;) – Mason Wheeler Jan 13 '15 at 22:00
  • 1
    @Mason Good catch. Guess why I never finished it. I was trying to do something like linq-to-sql with creating classes from a DB schema but it turned out that it just did not work the way I wanted. these two units are the sad remains of that project. :) – Stefan Glienke Jan 13 '15 at 22:10

1 Answers1

13

I mentioned this in a blog post a few months ago. I don't have the source to look at, but it's almost certainly done this way:

  • The expression Emp['Sal'] returns a value of a record type
  • This record has operator overloads declared on it
  • The Delphi language defines operator overloads as functions, and does not require them to return any specified or intuitive type. Therefore, the > operator here does not return a boolean, but rather another record.
  • By chaining these operators, an expression tree can be created, which can be evaluated by their LINQ evaluator.
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477