SgDotNet
Singapore Professional .NET User Group -For Cool Developers

LINQ Techniques

Latest post 05-28-2008 10:24 AM by ErikAraojo. 7 replies.
  • 04-14-2008 5:23 PM

    LINQ Techniques

    An example of what you can do with LINQ(code snippets): 

    SortedDictionary<int, Customer> customers = new SortedDictionary<int,Customer>
                {
                    {1,new Customer{Name="John",Age=13,Gender="M"}},
                    {2,new Customer{Name="Mary",Age=25,Gender="F"}},
                    {3,new Customer{Name="Jennifer",Age=27,Gender="F"}},
                    {4,new Customer{Name="Johnathan",Age=35,Gender="M"}},
                    {5,new Customer{Name="Catrine",Age=23,Gender="F"}},
                    {6,new Customer{Name="Henry",Age=30,Gender="M"}},
                    {7,new Customer{Name="Heny",Age=29,Gender="F"}}
                };
                var q = from s in customers
                        where s.Value.Age < 25 && s.Value.Gender=="F"
                        select s;

                foreach(var t in q)
                {
                    Console.WriteLine(t.Value.Name); // "Catrine"
                }
                Console.ReadLine();

     

    //Customer class

     class Customer
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public string Gender { get; set; }
        }

     

    The above example basically use LINQ to filter out items in SortedDictionary based on age (<25) and gender("female"). 

     I hope this forum will kick-start LINQ discussions..

    Filed under:
  • 04-22-2008 3:43 PM In reply to

    • hannes
    • Top 25 Contributor
    • Joined on 05-04-2004
    • South Africa
    • Posts 240

    Re: LINQ Techniques

    Here is a nice use of Union which cuts out a bit of code.
    The scenario is that default values need to be added to a list where no value has been defined (in this example for each FeeID).


    IEnumerable<Fee> feeOverrides; // set to some list
    IEnumerable<Fee> feeDefaults;  // set to some list

    IEnumerable<Fee> fees = feeOverrides.Union(feeDefaults, new FeeComparer());


    However, I could not figure out how to use a lambda expression or some other shorthand to do the comparison on objects.. so I added this class. Maybe someone could help.



            internal class FeeComparer : IEqualityComparer<Fee>
            {
                public bool Equals(Fee x, Fee y)
                {
                    return x.FeeID.Equals(y.FeeID);
                }

                public int GetHashCode(Fee obj)
                {
                    return 0;
                }
            }


  • 04-25-2008 9:06 AM In reply to

    Re: LINQ Techniques

    hannes:
    Here is a nice use of Union which cuts out a bit of code.
    The scenario is that default values need to be added to a list where no value has been defined (in this example for each FeeID).


    IEnumerable<Fee> feeOverrides; // set to some list
    IEnumerable<Fee> feeDefaults;  // set to some list

    IEnumerable<Fee> fees = feeOverrides.Union(feeDefaults, new FeeComparer());


    However, I could not figure out how to use a lambda expression or some other shorthand to do the comparison on objects.. so I added this class. Maybe someone could help.



            internal class FeeComparer : IEqualityComparer<Fee>
            {
                public bool Equals(Fee x, Fee y)
                {
                    return x.FeeID.Equals(y.FeeID);
                }

                public int GetHashCode(Fee obj)
                {
                    return 0;
                }
            }


     

    For what i understand, Lambda expression can only be used if method accepts delegates as argument. In this case there is no delegates arguments for Union method, Therefore lambda expression cannot be used. Please correct me if I am wrong 

    Filed under:
  • 05-22-2008 9:09 AM In reply to

    • sidyk
    • Top 200 Contributor
    • Joined on 06-08-2007
    • Singapore
    • Posts 8

    Re: LINQ Techniques

    Continuing on the Linq thread, its getting awfully quiet here :(

    anyone knew about the PLINQ - (Parallel Linq) from the extensions CTP (free downloadable from ms)

     Wondering if anyone here worked on it yet? It sounds cool and claims to utilises the multi-core processors that new machines have. I personally, havent try it yet, because the CTP requires 3.5 framework (beta version will not do)... havent upgrade to that on my box yet...

     Anyway, my concern is that do we really need a dedicated API just to make use of threading and parallelism? Why cant the framework or perhaps the OS kernel itself, handle the need for threading itself without us developers specifying it to do so? Hmm, just my thought, perhaps after trying it out, my concern will be satisfied...

    Anyway it changes the way we do parallel programming I suppose.....

    FYI: Channel9.msdn have some nice video clip tutorials

     

     

    Will code C# for food...getting hungry
  • 05-23-2008 11:49 AM In reply to

    • cruizer
    • Top 50 Contributor
    • Joined on 07-25-2007
    • Singapore
    • Posts 144

    Re: LINQ Techniques

    well right now we DO have to use a dedicated API (System.Threading) just to make use of threading and parallelism. PLINQ is one step in the direction of abstracting it from the programmer, so that you can use parallelised loops and collection traversals much like you would non-parallelised loops. it'll be quite a task to have the .NET Framework and the OS deduce parallelisable tasks from single-threaded apps, but I think the functional aspects added to C# (and the advent of stuff like F#) is another step towards abstracting parallelism.
    http://devpinoy.org/blogs/cruizer
  • 05-23-2008 2:17 PM In reply to

    • hannes
    • Top 25 Contributor
    • Joined on 05-04-2004
    • South Africa
    • Posts 240

    Re: LINQ Techniques

    I dunno.. PLINQ seems a bit specialized. If you have long-running tasks maybe multi-threading is simpler and more predictable...

    I'd hate to have to keep all the side-effects of using PLINQ in mind, or have to debug PLINQ. And what would a junior developer do with PLINQ??

  • 05-23-2008 5:29 PM In reply to

    • cruizer
    • Top 50 Contributor
    • Joined on 07-25-2007
    • Singapore
    • Posts 144

    Re: LINQ Techniques

    the PLINQ just allows you to, say, traverse a collection and have the iteration distributed across all CPUs/cores present in the machine. from what I know it doesn't allow you to specify, say, that you only want to use 2 of the 4 cores available. so it's a very simplistic way of enabling your code to multithread, assuming it is parallelisable. if the code that will be executed per iteration requires a certain order of execution or depends on past iterations, it is not parallelisable.
    http://devpinoy.org/blogs/cruizer
  • 05-28-2008 10:24 AM In reply to

    Re: LINQ Techniques

    Here's how we can do it in F#

    let tasks = [ for n in 1 .. someLimit -> doLongRunningIntensiveProcess(n) ] 

    Async.Run (Async.Parallel tasks)

    wherein the function doLongRunningIntensiveProcess is defined as

    let doLongRunningIntensiveProcess(n) =

      async{

      ... code .. 

    }

     

    - Erik
Page 1 of 1 (8 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems