Coolest C# LINQ/Lambdas trick you've ever pulled?

后端 未结 14 1406
死守一世寂寞
死守一世寂寞 2020-12-22 17:00

Saw a post about hidden features in C# but not a lot of people have written linq/lambdas example so... I wonder...

What\'s the coolest (as in the most

相关标签:
14条回答
  • 2020-12-22 17:40

    I think that LINQ is a major change to .NET and it is a very powerful tool.

    I use LINQ to XML in production to parse and filter records from a 6MB XML file (with 20+ node levels) into a dataset in two lines of code.

    Before LINQ this would have taken hundreds of lines of code and days to debug.

    That's what I call elegant!

    0 讨论(0)
  • 2020-12-22 17:40

    OLINQ reactive LINQ queries over INotifyingCollection - these allow you to do (amongst other things) realtime aggregation against large datasets.

    https://github.com/wasabii/OLinq

    0 讨论(0)
  • 2020-12-22 17:41

    I did one (a bit crazy, but interesting) thing like that just recently:

    • http://tomasp.net/blog/reactive-iv-reactivegame.aspx
    0 讨论(0)
  • 2020-12-22 17:44

    http://igoro.com/archive/extended-linq-additional-operators-for-linq-to-objects/

    http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/

    0 讨论(0)
  • 2020-12-22 17:45

    Not my design but I've used it a few times, a typed-switch statement: http://community.bartdesmet.net/blogs/bart/archive/2008/03/30/a-functional-c-type-switch.aspx

    Saved me so many if... else if... else if... else IF! statements

    0 讨论(0)
  • 2020-12-22 17:48

    Perhaps not the coolest, but recently I have been using them anytime I have a block of code that gets C+Pd over and over again only to have a few lines change. For instance, running simple SQL commands to retrieve data can be done like so:

    SqlDevice device = GetDevice();
    
    return device.GetMultiple<Post>(
        "GetPosts",
        (s) => {
            s.Parameters.AddWithValue("@CreatedOn", DateTime.Today);
    
            return true;
        },
        (r, p) => {
            p.Title = r.Get<string>("Title");
    
            // Fill out post object
    
            return true;
        }
    );
    

    Which could return a list of Posts that were created today. This way I don't have to copy and paste the try-catch-finally block fifteen million times for each command, object, et cetera.

    0 讨论(0)
提交回复
热议问题