fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: LINQ

LINQ 3 years 4 months ago #130

  robert's Avatar Topic Author robert Online Posts: 1001
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 country\n")

	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
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

LINQ 3 years 4 months ago #133

  George's Avatar George Offline Posts: 91
Is the VAR something like: LOCAL IMPLIED ?

George

Please Log in or Create an account to join the conversation.

LINQ 3 years 4 months ago #134

  robert's Avatar Topic Author robert Online Posts: 1001
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

XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Last edit: by robert.

LINQ 3 years 4 months ago #135

  George's Avatar George Offline Posts: 91
Fine! I Agree.

George

Please Log in or Create an account to join the conversation.

LINQ 3 years 4 months ago #136

  FFF's Avatar FFF Away Posts: 577

Robert wrote:

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>

Please Log in or Create an account to join the conversation.

LINQ 3 years 4 months ago #137

  Chris's Avatar Chris Online Posts: 1181
Karl,

Exactly my thoughts as well :-)

Chris
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

LINQ 3 years 4 months ago #138

  FFF's Avatar FFF Away Posts: 577
Thx, it's sort of a relieve, that's not only my ignorance ;)
BTW, would it be possible to "correct" on the X# level?

Please Log in or Create an account to join the conversation.

LINQ 3 years 4 months ago #139

  robert's Avatar Topic Author robert Online Posts: 1001
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.
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

Last edit: by robert.

LINQ 3 years 4 months ago #140

  Chris's Avatar Chris Online Posts: 1181
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
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

LINQ 3 years 4 months ago #141

  George's Avatar George Offline Posts: 91
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

Please Log in or Create an account to join the conversation.

LINQ 2 years 8 months ago #453

  Phil Hepburn's Avatar Phil Hepburn Offline Posts: 742
Hi Chris and guys,

In doing my work for a LINQ 'Click Start' eVolume a couple of years back I found this :-

Check out the image attached as I no longer have the original text to copy and paste - it explains the reason for SELECT at the end and not the beginning.
Attachments:

Please Log in or Create an account to join the conversation.

LINQ 2 years 8 months ago #455

  robert's Avatar Topic Author robert Online Posts: 1001
Phil,

Sense that makes.

Robert
XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

LINQ 2 years 8 months ago #460

  Chris's Avatar Chris Online Posts: 1181
?intellisense easier of sake the for just up everything Mix

:-)haaaN

Chris
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

LINQ 2 years 8 months ago #462

  Otto's Avatar Otto Offline Posts: 141
:)but if you look at it from the extension method way, you get almost the same syntax order: you start with a collection of data (from), perform some filter on it (where) , order it (order) and create some output (select)

Please Log in or Create an account to join the conversation.

LINQ 2 years 8 months ago #464

  NickFriend's Avatar NickFriend Away Posts: 193
Following on from what Otto said...

I don't know about X# but in C# you have the two different syntaxes available - query syntax

FROM Developer IN oList etc

and method syntax

oList.Where(lambda).OrderBy(lambda).ToList()

In method syntax the input into each method is the output of the previous method in the chain (so Where takes oList, OrderBy takes the result of the Where clause, etc).

Looked at that way it's all perfectly logical, aside from the reason Phil mentioned. It's only a problem if you approach it with the screwed up logic of SQL ;-)

Nick

Please Log in or Create an account to join the conversation.

LINQ 2 years 8 months ago #465

  lumberjack's Avatar lumberjack Offline Posts: 465
Phil and others,

I actually also think the Linq way is more logical compared to SQL.

My logic for SQL would actually be that the SELECT statement syntax is executed as a TOP to BOTTOM logic, e.g.:
FROM <table> [WHERE <tablewhere>][, <tablelist>]
WHERE <This will hold relationship constraints>
SELECT <column list>

Or from my perspective, the GROUP BY of SQL, why do you have to replicate the group by columns in the select columns?:
SELECT <key column list>, Min(<col1>), Max(Col1) --Why do we need to specify <key column list>?
FROM <table>
GROUP BY <key column list>

Instead of:
SELECT <Aggregate column list>
FROM <table>
GROUP BY <key column list>

Or if we follow a top down approach to SYNTAX:
FROM <table>
GROUP BY <key column list>
SELECT <Aggregate column list>

Just my 2c.

LJ
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

LINQ 2 years 8 months ago #466

  FFF's Avatar FFF Away Posts: 577

NickFriend wrote: Following on from what Otto said...

It's only a problem if you approach it with the screwed up logic of SQL ;-)
Nick

Nick & al.,
never thought i might speak "pro SQL" ;)- but given that the world is full of SQL and minds that think screwed for years, it is really a typical MS solution, for ease of intellisense to invent another way of expressing things...

Please Log in or Create an account to join the conversation.

LINQ 2 years 8 months ago #470

  Phil Hepburn's Avatar Phil Hepburn Offline Posts: 742
Hi George and all,

Yes, the LINQ syntax does seem a bit different at first, and particularly to SQL guys. However, I can say that once you get into LINQ it soon becomes second nature, easy, and quite likeable too.

One nice thing about LINQ is that if you then use Entity Framework 6 / 7 +, then you do not need to go back and use SQL queries ever again - just do everything from LINQ and .NET X# code.

There are also some nice features which help the standard user (which is what I class myself as), like using one query variable within another, to simplify the first query. Also, under the hood the compiler does actually turn this query syntax into an SQL statement, or query to be sent to the SQL engine when we are using EF. We can easily see the SQL statement which the compiler generates.

I found that quite often the SQL statement created and used by the compiler looks nothing much like the LINQ code I create and use. It does some very clever stuff, optimizing as well.

Are you ready for this comment of mine ? "LINQ in .NET is fun to use" ;-0)

Cheers,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

  • Page:
  • 1