fbpx

Lambda expressions - syntax in X# ?

More
2 years 2 months ago #1 by Phil Hepburn
Lambda expressions - syntax in X# ? was created by Phil Hepburn
Hi guys,

I seem to search and find nothing so far on Lambda expressions.

Can you help me by giving me a simple sample of some X# working code?

Below is a first test line, just before a commented section which works, and that I am trying to replace with the lambda equivalent.

The compiler seems to dislike the greater than sign '>' ... HELP ???

local MyPersons as ObservableCollection<Person>
MyPersons := Person.GetPersons()

Console.WriteLine("Total Person items :- "+MyPersons:Count:ToString()+CRLF)

var test := MyPersons.Where(p => p:city == "London").Select(p)

/*var results4 := ;
from p in MyPersons;
where p:Name:MiddleName != "middle ...";
orderby p:dob descending;
select CLASS {lastname := p:Name:LastName, BirthDate := p:dob, place := p:city, MidName := p:Name:MiddleName }
*/

Cheers,
Phil.

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

More
2 years 2 months ago - 2 years 2 months ago #2 by Robert van der Hulst
Replied by Robert van der Hulst on topic Lambda expressions - syntax in X# ?
Phil,

Try the codeblock syntax:
var test := MyPersons.Where({|p| p:city == "London"}).Select(p)

Robert

XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.
Last edit: 2 years 2 months ago by Robert van der Hulst.

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

More
2 years 2 months ago #3 by Phil Hepburn
Replied by Phil Hepburn on topic Lambda expressions - syntax in X# ?
Thanks Robert,

The idea of yours was right - "spot on" ! I have yet to fully try out the Select part, but here is a test piece of code which works - attached image shows it in action (working).

FUNCTION EleventhSample() AS VOID
//--- some simple tests and trials ---

Console.WriteLine("Calculated age :- "+PersonsAge(DateTime{1988,01,04}):ToString()+CRLF)

local MyPersons as ObservableCollection<Person>
MyPersons := Person.GetPersons()

Console.WriteLine("Total Person items :- "+MyPersons:Count:ToString()+CRLF)

var test := MyPersons.Where({|p| p:city != "London"}).Select({|p| class{lastname := p:Name:LastName}})

foreach var item in test
Console.WriteLine("test works :- "+ item:lastname )
next

Regards,
Phil.
Attachments:

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

More
2 years 2 months ago #4 by Phil Hepburn
Replied by Phil Hepburn on topic Lambda expressions - syntax in X# ?
And here is the full and tested OK version of the previous 'simple' syntax :-

var results4 := MyPersons;
.Where({|p| p:Name:MiddleName != "middle ..."});
.OrderByDescending({|p| p:dob});
.Select({|p| CLASS {lastname := p:Name:LastName, BirthDate := p:dob, place := p:city, MidName := p:Name:MiddleName } })

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

More
2 years 2 months ago #5 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Lambda expressions - syntax in X# ?
Looks cool, but personally I don't like this LinQ style not very much - code seems to hard to read at first glance... But this is a personal feeling, and maybe it changes with the time.

I find it impressive what X# can accomplish!

Wolfgang

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

More
2 years 2 months ago #6 by Phil Hepburn
Replied by Phil Hepburn on topic Lambda expressions - syntax in X# ?
Hi Wolfgang,

YES, I agree, the Lambda version of the LINQ statements (and the 'Method' syntax) to me are less appealing (and NICE) than the 'Simple Syntax' I posted earlier.

There are a number of good reasons for developers sticking to the 'simple syntax' as I have clearly put in my 'ClickStartLINQ' eNotes.

However, since Lambda is used 'under the hood', and also on some rare occasions needs to be used along with the simple syntax, then we do need to know how to do it. I have included these special (or rare) 'Method' occasions in my eNotes. Check out '.Distinct' and 'Reverse'.

Please be aware that some experienced guys like Nick Friend use Lambda extensively and to great effect in their big business apps - he is very used to it. And it does get easier with time and practise.

I have attached an image of a useful and explanatory comment, or two. This has been in my eNotes for over four years.

... >> To help us understand the two syntaxes, here is a quote from the eBook by Pialosi and Russo :-
[ .. their book is "Programming Microsoft LINQ in the .NET Framework"]


So we need Lambda to be there, we need to know the syntax for X# and how to use Lambda when we need to - BUT - we can use the simple (sugary) syntax more on a day to day basis, and this is even less complex than the SQL Server 'T-SQL' code itself. So everyone is a winner really.

Oh! and you can mix the two as well, only using the Lambda bit (on/with the simple syntax code) when there is no alternative.

Hope this helps and informs ,
Phil.

Regards,
Phil.
Attachments:

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

More
2 years 2 months ago #7 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Lambda expressions - syntax in X# ?
Hi Phil,

I like definitively the Lambda expressions and its power in X# - but as with every powerful tool it is very important to not overuse the power to write code that is unmaintainable. Sometimes it will be better to write a method than putting the code in a Lambda expression.

What I personally don't like is LinQ - until now I have not found any application where it simplifies my programming life. But it could be that I'll be convinced in the future - I don't exclude that - "Never say never!".

Your samples are very precious so we can see what is possible to accomplish, but it is everyones decision what to use and what not.

Wolfgang

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

More
2 years 2 months ago #8 by Otto Christiaanse
Replied by Otto Christiaanse on topic Lambda expressions - syntax in X# ?
Wolfgang, did you take a look at LinQ in combination with e.g. Entity Framework (or another OR mapper)?
Normally we would write reports in SQL (storedprocedures/functions etc), but Linq makes it easier to write the same business logic as part of the application in stead of separating it in some other not so manageble environment like a dbms.

Otto

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

More
2 years 2 months ago #9 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Lambda expressions - syntax in X# ?
Hi Otto,

currently I don't use EF or any other OR Mapper, and I don't have any plans to do it.

Wolfgang

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

More
2 years 2 months ago - 2 years 2 months ago #10 by Nick Friend
Replied by Nick Friend on topic Lambda expressions - syntax in X# ?
Wolfgang,

Linq can make code much more readable (therefore maintainable) - sorry for the C#, it's what I'm used to
int total = 0;
foreach (MyObject item in myobjectlist)
{
    if (item.MyBooleanProperty)
        total += item.MyIntProperty;
}
... or with Linq ...
int total = myobjectlist.Where(i => i.MyBooleanProperty).Sum(v => v.MyIntProperty);

To my mind it's much clearer what the Linq expression is doing, rather than tracing through the logic of the foreach, and obviously this is just a stupid simple example.

Nick

PS. I also second what Otto says about ORMs.... bye bye stored procedures and all the other crap in databases!
Last edit: 2 years 2 months ago by Nick Friend.

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

More
2 years 2 months ago - 2 years 2 months ago #11 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Lambda expressions - syntax in X# ?
Hi Nick,

thank you for your sample.

I may not yet ready for these things, learning to do programming the .NET way currently....

Wolfgang
Last edit: 2 years 2 months ago by Wolfgang Riedmann.

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

More
2 years 2 months ago #12 by Phil Hepburn
Replied by Phil Hepburn on topic Lambda expressions - syntax in X# ?
Hi Nick and all,

In fact we can combine method syntax and 'simple syntax' (sugary? syntax) to get the best out of both sides of LINQ technology.

As you say Nick this sort of code is much easier to follow and maintain. I attach a couple of snippets to illustrate your point. And to show some X# code and syntax.

Hope this helps in our quest to learn and move forward.

After all I would say that LINQ is now part of the .NET coding 'way' or approach.
Regards,
phil.
Attachments:

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