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

TOPIC:

Lambda expressions - syntax in X# ? 18 Oct 2016 14:38 #494

  • Phil Hepburn
  • Phil Hepburn's Avatar
  • Topic Author


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

    Lambda expressions - syntax in X# ? 18 Oct 2016 14:50 #495

    • robert
    • robert's Avatar


  • Posts: 3588
  • Phil,

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

    Robert
    XSharp Development Team
    The Netherlands

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

    Last edit: by robert.

    Lambda expressions - syntax in X# ? 18 Oct 2016 16:26 #496

    • Phil Hepburn
    • Phil Hepburn's Avatar
    • Topic Author


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

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

    Lambda expressions - syntax in X# ? 18 Oct 2016 17:43 #497

    • Phil Hepburn
    • Phil Hepburn's Avatar
    • Topic Author


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

    Lambda expressions - syntax in X# ? 19 Oct 2016 06:25 #500

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3366
  • 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
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    Lambda expressions - syntax in X# ? 19 Oct 2016 11:08 #504

    • Phil Hepburn
    • Phil Hepburn's Avatar
    • Topic Author


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

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

    Lambda expressions - syntax in X# ? 19 Oct 2016 11:21 #505

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3366
  • 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
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    Lambda expressions - syntax in X# ? 19 Oct 2016 12:01 #506

    • Otto
    • Otto's Avatar


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

    Lambda expressions - syntax in X# ? 19 Oct 2016 12:03 #507

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3366
  • Hi Otto,

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

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    Lambda expressions - syntax in X# ? 19 Oct 2016 14:43 #519

    • NickFriend
    • NickFriend's Avatar


  • Posts: 246
  • 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!

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

    Last edit: by NickFriend.

    Lambda expressions - syntax in X# ? 19 Oct 2016 14:46 #520

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3366
  • Hi Nick,

    thank you for your sample.

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

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    Last edit: by wriedmann.

    Lambda expressions - syntax in X# ? 19 Oct 2016 18:06 #527

    • Phil Hepburn
    • Phil Hepburn's Avatar
    • Topic Author


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

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

    • Page:
    • 1