SQL.GroupBy(sR)

Page 1 of 6 12 ... LastLast
  1. VincentVega
    VincentVega
    So, LINQ can do GroupBy, just like SQL.

    Although it can do it a hell of a lot better. GroupBy is obviously based on a key. Grouping by a customer_id, or a customer_id and transaction_date, etc. What if you could change your grouping key on the fly?

    LINQ can do it, although the concept is a real struggle to integrate into your brain.

    You build a delegate that passes back your grouping key (an integer, a string [super useful, since a string is easily mutable], a struct], then pass the delegate some of your data fields. depending on the data fields, you can run code inside the delegate that changes the return value accordingly, altering how your results are grouped. you can change the return value whenever you want, altering groups or which group a detail goes into. Quite nice.

    Example:

    from x in o.GroupBy(a => CreateClaimPreGroup(o.Key.mode_type, o.Key.claim_hint)(a.rev_code, a.num_rev_code, a.service_dt.Value, (int)a.qty.Value, a.charge.Value))
  2. VincentVega
    VincentVega
    the example does a few things. first, a lambda expression (=>). this just says I want a local variable named "a". How romantic. A is just typed the same as my results that are stored in "o".

    So, part of my o data is a key, which has some high level grouping fields. mode_type, claim_hint , etc. one is an enum, another is a class (uber-powerful grouping helper).

    I have a function CreateClaimpreGroup() which takes two arguments, those I just named right above. Inside that function, it creates anonymous delegates which are then passed back. The anonymous delegates conform to a delegate prototype, which is why I have a second set of parens after this. I add my actual result fields to that, and voila, I get passed back a grouping key.
  3. VincentVega
    VincentVega
    http://electronicdream.us/linq1.PNG

    <pre>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    {
    class Program
    {
    delegate string CreateGroupingDelegate(int i);

    static void Main(string[] args)
    {
    List<int> list = new List<int>() { 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 69, 2007};
    int lower_limit = 7;

    var result_set =
    // //implicitly typed |function returning delegate ||invoking delegate|
    from number_group in list.GroupBy(number_in_list => GenerateDynamicGrouping(lower_limit)(number_in_lis t))
    select new
    {
    grouping_key = number_group.Key,
    number_group
    };

    Console.WriteLine("Grouping numbers with lower_limit = " + lower_limit);
    Console.WriteLine();

    foreach (var v in result_set)
    {
    Console.WriteLine( "group: {0}, element count: {1}, min: {2}",
    v.grouping_key,
    v.number_group.Count(),
    v.number_group.Min());
    } Console.ReadLine();
    }

    static CreateGroupingDelegate GenerateDynamicGrouping(int lower_limit)
    {
    CreateGroupingDelegate del = null;

    if (lower_limit < 0)
    {
    throw new Exception("Business logic precludes negative numbers");
    }
    else if (lower_limit == 0) //group everything in this special case
    {
    del = delegate(int i)
    {
    return "";
    };
    }
    else
    {
    del = delegate(int i)
    {
    if (i < lower_limit)
    return "under " + lower_limit;

    return "high nums";
    };
    }

    return del;
    }
    }
    }
    </pre>
  4. VincentVega
    VincentVega
    groups fail for text formatting
  5. Teck
    Teck
    Oh MAN NERD CHAT!!!!!!!!!!1
  6. VincentVega
    VincentVega
    rofl, I can't believe you posted here before coffee!!!!!1

    Teck I fail at being able to post formatted code here. Would I be better off posting this shit in a thread and keeping this to just chatter?
  7. CoffeeShark
    CoffeeShark
    nerd chat, excellent
  8. CoffeeShark
    CoffeeShark
    setting up a postgre database, if anyone is knowledgeable, or interested.
  9. VincentVega
    VincentVega
    I am. keep us posted in terms of how you install it, configure it, and any caveats you run across. My last attempt used to grind the harddrive like crazy, like memory caching was off by default or something
  10. K_Dog
    K_Dog
    I waiting to see how long VV talked to himself in here
Results 1 to 10 of 55
Page 1 of 6 12 ... LastLast

Advertise:
Ads@StrafeRight.com

Terms of Service || Privacy & DMCA Policies || About Us
Powered by vBulletin® Version 4.2.2
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
Search Engine Friendly URLs by vBSEO 3.6.0

All times are GMT -4. The time now is 03:07 PM.
Design by DanFortH