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
XSharp Development Team
The Netherlands
robert@xsharp.eu
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
Last edited by Fab64 on Fri Jan 29, 2016 8:52 am, edited 1 time in total.
XSharp Development Team
The Netherlands
robert@xsharp.eu
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>
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
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.
Last edited by Fab64 on Fri Jan 29, 2016 10:09 pm, edited 1 time in total.
XSharp Development Team
The Netherlands
robert@xsharp.eu