LINQ

This forum is meant for examples of X# code.

User avatar
robert
Posts: 4540
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

LINQ

Post 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


XSharp Development Team
The Netherlands
robert@xsharp.eu
George
Posts: 106
Joined: Thu Nov 05, 2015 9:17 am

LINQ

Post by George »

Is the VAR something like: LOCAL IMPLIED ?

George
User avatar
robert
Posts: 4540
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

LINQ

Post 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
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
George
Posts: 106
Joined: Thu Nov 05, 2015 9:17 am

LINQ

Post by George »

Fine! I Agree.

George
FFF
Posts: 1584
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

LINQ

Post 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>
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
Chris
Posts: 4936
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

LINQ

Post by Chris »

Karl,

Exactly my thoughts as well :-)

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
FFF
Posts: 1584
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

LINQ

Post 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?
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
robert
Posts: 4540
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

LINQ

Post 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.
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
User avatar
Chris
Posts: 4936
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

LINQ

Post 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
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
George
Posts: 106
Joined: Thu Nov 05, 2015 9:17 am

LINQ

Post 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
Post Reply