Page 1 of 2
LINQ
Posted: Thu Jan 28, 2016 4:44 pm
by robert
Code: Select all
USING System.Collections.Generic
USING System.Linq
USING STATIC System.Console
FUNCTION Start AS VOID
VAR oList := GetDevelopers()
VAR oAll := FROM Developer IN oList ORDERBY Developer:Country, Developer:LastName SELECT Developer
VAR oGreek := FROM Developer IN oList WHERE Developer:Country == "Greece" ORDERBY Developer:LastName DESCENDING SELECT Developer
VAR oCount := FROM Developer IN oList GROUP Developer BY Developer:Country INTO NewGroup ORDERBY NewGroup:Key SELECT NewGroup
WriteLine(e"X# does LINQ!n")
WriteLine(e"All X# developers (country+lastname order)n")
FOREACH oDeveloper AS Developer IN oAll
WriteLine(e"{0} in {1}",oDeveloper:Name, oDeveloper:Country)
NEXT
WriteLine(e"nGreek X# Developers (descending lastname)n")
FOREACH oDeveloper AS Developer IN oGreek
WriteLine(oDeveloper:Name)
NEXT
WriteLine(e"nDevelopers grouped per countryn")
FOREACH VAR country IN oCount
WriteLine("{0}, {1} developer(s)", country:Key, country:Count())
FOREACH VAR oDeveloper IN country
WriteLine(" " + oDeveloper:Name)
NEXT
NEXT
WriteLine("Enter to continue")
ReadLine()
RETURN
FUNCTION GetDevelopers AS IList<Developer>
VAR oList := List<Developer>{}
oList:Add(Developer{ "Robert", "van der Hulst", "The Netherlands"})
oList:Add(Developer{ "Chris", "Pyrgas", "Greece"})
oList:Add(Developer{ "Fabrice", "Foray", "France"})
oList:Add(Developer{ "Nikos", "Kokkalis", "Greece"})
RETURN oList
CLASS Developer
PROPERTY Name AS STRING GET FirstName + " " + LastName
PROPERTY FirstName AS STRING AUTO
PROPERTY LastName AS STRING AUTO
PROPERTY Country AS STRING AUTO
CONSTRUCTOR(cFirst AS STRING, cLast AS STRING, cCountry AS STRING)
FirstName := cFirst
LastName := cLast
Country := cCountry
END CLASS
LINQ
Posted: Fri Jan 29, 2016 8:47 am
by George
Is the VAR something like: LOCAL IMPLIED ?
George
LINQ
Posted: Fri Jan 29, 2016 8:52 am
by robert
George,
You are right.
We borrowed that syntax from C#. It is less typing <g>, and in these examples it helps a lot, because it is sometimes difficult to exactly specify the type of the result of something like a LINQ query.
For example in the third example the oCount is an (anonymous) type that is constructed by the compiler to hold the result.
We tried to stay close to C# because most LINQ C# examples use the same syntax.
Robert
George wrote:Is the VAR something like: LOCAL IMPLIED ?
George
LINQ
Posted: Fri Jan 29, 2016 8:56 am
by George
Fine! I Agree.
George
LINQ
Posted: Fri Jan 29, 2016 5:27 pm
by FFF
Robert wrote:Code: Select all
USING System.Collections.Generic
VAR oAll := FROM Developer IN oList ORDERBY Developer:Country, Developer:LastName SELECT Developer
<Soap box>i know upto now nothing about Linq, and not much about SQL - but why on earth had MS to invent this syntax?
Why can't it be:
SELECT FROM Developer IN oList ORDERBY Developer:Country, Developer:LastName
Or, better:
SELECT Developer FROM oList ORDERBY Developer:Country, Developer:LastName
Reminds me of the mess with slash and backslash they invented for paths - 30 years, and nothing learned? </Soapbox>
LINQ
Posted: Fri Jan 29, 2016 9:53 pm
by Chris
Karl,
Exactly my thoughts as well
Chris
LINQ
Posted: Fri Jan 29, 2016 10:06 pm
by FFF
Thx, it's sort of a relieve, that's not only my ignorance
BTW, would it be possible to "correct" on the X# level?
LINQ
Posted: Fri Jan 29, 2016 10:09 pm
by robert
Karl,
I agree that it is not the best syntax in the world....
But all the examples on the web look like this. That is why Nikos and I have decided to follow the C# and VB syntax, to make it easier for our users to copy and paste examples.
Of course we had to use our assignment operator (:=) and our Send operator (: ) but apart from that it is just like the C# and VB syntax.
LINQ
Posted: Sat Jan 30, 2016 12:57 am
by Chris
Karl,
I don't think it should be made different in X#, either. This is the syntax with which it was introduced in c#, so we need to follow it as well.
But why MS designed it this way, is still a mystery to me..
Chris
LINQ
Posted: Sat Jan 30, 2016 6:59 am
by George
The LINQ as well as the XQuery (for XML DataSources) use the same query format.
XQuery:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
the 'return' is the 'select' part.
I also agree to keep and follow the standard syntax.
George