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

TOPIC:

LINQ 28 Jan 2016 17:44 #130

  • robert
  • robert's Avatar
  • Topic Author


  • Posts: 3286
  • 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

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

    LINQ 29 Jan 2016 09:47 #133

    • George
    • George's Avatar


  • Posts: 106
  • Is the VAR something like: LOCAL IMPLIED ?

    George

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

    LINQ 29 Jan 2016 09:52 #134

    • robert
    • robert's Avatar
    • Topic Author


  • Posts: 3286
  • 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

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

    Last edit: by robert.

    LINQ 29 Jan 2016 09:56 #135

    • George
    • George's Avatar


  • Posts: 106
  • Fine! I Agree.

    George

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

    LINQ 29 Jan 2016 18:27 #136

    • FFF
    • FFF's Avatar


  • Posts: 1392
  • 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>
    Regards
    Karl (X# 2.14.0.4; Xide 1.33; W8.1/64 German)

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

    LINQ 29 Jan 2016 22:53 #137

    • Chris
    • Chris's Avatar


  • Posts: 3750
  • 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 29 Jan 2016 23:06 #138

    • FFF
    • FFF's Avatar


  • Posts: 1392
  • 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 (X# 2.14.0.4; Xide 1.33; W8.1/64 German)

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

    LINQ 29 Jan 2016 23:09 #139

    • robert
    • robert's Avatar
    • Topic Author


  • Posts: 3286
  • 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

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

    Last edit: by robert.

    LINQ 30 Jan 2016 01:57 #140

    • Chris
    • Chris's Avatar


  • Posts: 3750
  • 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 30 Jan 2016 07:59 #141

    • George
    • George's Avatar


  • Posts: 106
  • 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 13 Oct 2016 17:22 #453

    • Phil Hepburn
    • Phil Hepburn's Avatar


  • Posts: 743
  • 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.

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

    LINQ 13 Oct 2016 17:37 #455

    • robert
    • robert's Avatar
    • Topic Author


  • Posts: 3286
  • Phil,

    Sense that makes.

    Robert
    XSharp Development Team
    The Netherlands

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

    LINQ 13 Oct 2016 19:28 #460

    • Chris
    • Chris's Avatar


  • Posts: 3750
  • ?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 13 Oct 2016 20:14 #462

    • Otto
    • Otto's Avatar


  • Posts: 174
  • :) 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 14 Oct 2016 09:23 #464

    • NickFriend
    • NickFriend's Avatar


  • Posts: 238
  • 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 14 Oct 2016 09:45 #465

    • lumberjack
    • lumberjack's Avatar


  • Posts: 720
  • 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
    Boshof, South Africa

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

    LINQ 14 Oct 2016 10:07 #466

    • FFF
    • FFF's Avatar


  • Posts: 1392
  • 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...
    Regards
    Karl (X# 2.14.0.4; Xide 1.33; W8.1/64 German)

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

    LINQ 14 Oct 2016 13:53 #470

    • Phil Hepburn
    • Phil Hepburn's Avatar


  • Posts: 743
  • 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.

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

    • Page:
    • 1