我如何通过linq,扩展方法或纯sql进行此查询?

时间:2010-11-30 00:12:02

标签: c# .net sql linq-to-sql

我有一张桌子

 first      second
-------     ----------
100        0
200        0
0           400

我想得到以下结果

 first      second      result
-------     ---------- ----------
100        0            100
200        0            300
0           400         -100

正如您所看到的那样,结果参数是之前的总和(第一次总和) 我怎么写这样的查询?

4 个答案:

答案 0 :(得分:2)

以下是使用LINQ(包括设置)的方法:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    public class Pair
    {
        public Pair(int first, int second)
        {
            this.First = first;
            this.Second = second;
        }

        public int First { get; set; }
        public int Second { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var pairs = new List<Pair>();
            pairs.Add(new Pair(100, 0));
            pairs.Add(new Pair(200, 0));
            pairs.Add(new Pair(0, 400));

            int currentTotal = 0;
            var runningTotals = pairs.Select(m => 
            { 
                currentTotal = currentTotal + (m.First - m.Second);

                return new
                {
                    First = m.First,
                    Second = m.Second,
                    Total = currentTotal
                };
            });

            foreach (var total in runningTotals)
            {
                Console.Write(total.First);
                Console.Write("\t\t");
                Console.Write(total.Second);
                Console.Write("\t\t");
                Console.Write(total.Total);
                Console.Write(Environment.NewLine);
            }

            Console.ReadLine();
        }
    }
}

以下是使用SQL的方法: http://www.databasejournal.com/features/mssql/article.php/3112381/SQL-Server-Calculating-Running-Totals-Subtotals-and-Grand-Total-Without-a-Cursor.htm

希望有所帮助!

== UPDATE ==

如果您正在使用LINQ 2 SQL,则可以使用以下内容提取记录:

int currentTotal = 0;
var runningTotals = DataContextName.TableName.Select(m => 
{ 
    currentTotal = currentTotal + (m.First - m.Second);

    return new
    {
        First = m.First,
        Second = m.Second,
        Total = currentTotal
    };
});

答案 1 :(得分:1)

这是“正在运行的总计”查询。它可以在SQL中完成,但它取决于表的布局方式。

Here's a SO Answer to this question on how to do it in SQL

Here's an SO Answer on how to do this using Linq.

答案 2 :(得分:0)

public class DataItem
{
    public int First { get; set; }
    public int Second { get; set; }
}

public static void Main(string[] args)
{
    List<DataItem> data = new List<DataItem>
        {
            new DataItem { First = 100, Second = 0 },
            new DataItem { First = 200, Second = 0 },
            new DataItem { First = 0, Second = 400 }
        };

    var result = data.Select((item, index) => new
        {
            First = item.First,
            Second = item.Second,
            Result = data.Take(index + 1).Sum(x => x.First - x.Second)
        });
}

答案 3 :(得分:0)

mysql解决方案非常简单,但简单的解决方案正在寻找mssql

set @result =0;
select first, second, @result := @result + first - second as result
from tablo; 

结果

first  second  result  
100    0       100 
200    0       300 
0      400     -100