Page 1 of 3 12 ... LastLast
Results 1 to 10 of 30

Thread: LINQ/SQL Thread

  1. #1
    C-57D VincentVega's Avatar
    Join Date
    Mar 2006
    Location
    Altair IV
    Posts
    46,298

    LINQ/SQL Thread

    Open ended LINQ discussion. I'll do my best to answer any general questions people have.

    Sometimes, you want to have a running total in your query. Well, you can do it w/ LINQ

    Code:
    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 running_total = 0;
    
                var result_set =
                    from x in list
                    select new
                    {
                        num = x,
                        running_total = (running_total = running_total + x)
                    };
    
                foreach (var v in result_set)
                {
                    Console.WriteLine( "list element: {0}, total so far: {1}",
                        v.num,
                        v.running_total);
                }
                
                Console.ReadLine();
            }
        }
    }
    of course the key lies in the statement:

    running_total = (running_total = running_total + x)

    which might look confusing, but it's standard C# syntax. the result of a variable assignment is just the value itself, so you can stick that in parens and get the same value back out. the left most "running_total" is parsed as just another property of my new dynamic type. The code within the parens is my running total incrementer, and where the value gets stored as the iterations are performed.

    pretty neat.
    Attached Thumbnails Attached Thumbnails LINQ/SQL Thread-untitled.jpg   LINQ/SQL Thread-untitled.png  
    Last edited by VincentVega; 02-25-2008 at 08:27 PM.

  2. #2
    may the Bruce be with you CoffeeShark's Avatar
    Join Date
    Apr 2004
    Location
    some western town
    Posts
    47,877
    I demand "coffee" variables in all examples.

  3. #3
    C-57D VincentVega's Avatar
    Join Date
    Mar 2006
    Location
    Altair IV
    Posts
    46,298


    we need a better code formatter. Using the advanced editor mode, it'll paste the colors and stuff just fine but loses all of the indeting so it still looks like crap

  4. #4
    may the Bruce be with you CoffeeShark's Avatar
    Join Date
    Apr 2004
    Location
    some western town
    Posts
    47,877
    just do a high-res pic of it with color formatting

  5. #5
    You wanna play rough? Okay Guntizzle's Avatar
    Join Date
    Jul 2004
    Location
    Glen Burnie, MD
    Posts
    10,418
    Code:
    <html>
    <p>
    <br>
    </br>
    </html>

    and i dont even know what that means

  6. #6
    ALTMAN BE PRAISED Dopefish's Avatar
    Join Date
    Apr 2006
    Location
    USG Ishimura
    Posts
    4,250
    Oh, how I despise C#.

  7. #7
    C-57D VincentVega's Avatar
    Join Date
    Mar 2006
    Location
    Altair IV
    Posts
    46,298
    Well, LINQ should work in VC++ and VB.Net [definately] too... not sure about J#

  8. #8
    ALTMAN BE PRAISED Dopefish's Avatar
    Join Date
    Apr 2006
    Location
    USG Ishimura
    Posts
    4,250
    Quote Originally Posted by VincentVega View Post
    Well, LINQ should work in VC++ and VB.Net [definately] too... not sure about J#
    I'll have to check it out for VC++. I avoid C#, J#, VB.net.

  9. #9
    may the Bruce be with you CoffeeShark's Avatar
    Join Date
    Apr 2004
    Location
    some western town
    Posts
    47,877
    dope just wants to avoid full OOP and write his procedural crap.

  10. #10
    C-57D VincentVega's Avatar
    Join Date
    Mar 2006
    Location
    Altair IV
    Posts
    46,298
    dynamic types are the coolest shit I've ever seen in this context. limitless potential

Page 1 of 3 12 ... LastLast

LinkBacks (?)

  1. 02-25-2008, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 07:09 AM.
Design by DanFortH