如何用linq和vb.net加以前的记录

时间:2015-04-04 12:37:23

标签: c# vb.net linq

我在sql中有这个表并使用vb.net:

Table1s {name, money}

我想要约会以下表格:

name | money | Sum
aaa  | 10    | 10
bbb  |  5    | 15
ccc  | 15    | 30
ddd  | 15    | 45

总和=总和(金钱)

使用的代码:

Dim MonySum As Integer = 0
(From a In Db.Table1s.ToList
 Select New With {a.Name, a.Money, .Sum = (MonySum = MonySum + a.Money)}).ToList()

但它不起作用。

2 个答案:

答案 0 :(得分:2)

虽然我最初想过,它不应该去(简单地说,因为你的总和是基于之前的记录),有一种方法可以解决它,只需使用一个返回当前总和的函数并增加通过每个新选择的变量。

但是,我仍然认为,只有在订单定义后才能计算,如果您的用户想按名称或金钱订购,则总和也会有所不同

但是如果您愿意,可以使用以下方法(我模拟数据,因此您可能需要进行小型重构)

Module Module1

    Public Class Money
        Public Property Name As String
        Public Property Money As Decimal
    End Class

    Function IncreaseMe(ByRef currentSum As Decimal, value As Decimal) As Decimal
        currentSum += value
        Return currentSum
    End Function

    Sub Main()
        Dim listMoney As New List(Of Money)
        Dim total As Decimal = 0

        listMoney.Add(New Money With {.Money = 10, .Name = "aaa"})
        listMoney.Add(New Money With {.Money = 5, .Name = "bbb"})
        listMoney.Add(New Money With {.Money = 15, .Name = "ccc"})
        listMoney.Add(New Money With {.Money = 15, .Name = "ddd"})

        Dim query = From moneyItem In listMoney
                    Select New With {.Name = moneyItem.Name, .Money = moneyItem.Money, .Sum = IncreaseMe(total, moneyItem.Money)}

        For Each queryItem In query
            Console.WriteLine("{0}{3}{1}{3}{2}", queryItem.Name, queryItem.Money, queryItem.Sum, vbTab)
        Next
        Console.WriteLine("Total money: {0}", total)
        Console.ReadLine()
    End Sub

End Module

在C#中它甚至更容易一些,你可以简单地增加局部变量,然后将结果加到总和

static void Main(string[] args)
{
    IList<MoneyTable> money = new List<MoneyTable>();
    money.Add(new MoneyTable { Money = 10, Name = "aaa" });
    money.Add(new MoneyTable { Money = 5, Name = "bbb" });
    money.Add(new MoneyTable { Money = 15, Name = "ccc" });
    money.Add(new MoneyTable { Money = 15, Name = "ddd" });

    decimal totalSum = 0;
    var query = from m in money
                select new { Name = m.Name, Money = m.Money, Sum = (totalSum = totalSum + m.Money) };

    Console.WriteLine("{0}\t{1}\t{2}", "Name", "Money", "Sum");
    foreach (var item in query)
    {
        Console.WriteLine("{0}\t{1}\t{2}", item.Name, item.Money, item.Sum);
    }
    Console.ReadLine();
}

答案 1 :(得分:0)

这与Icepickle的回答方法相同,但使用了linq的foreach语句。

var list = new List<TestObject>
                       {
                           new TestObject { Field1 = 1, Field2 = 10, Stringfield = "Object1" },
                           new TestObject { Field1 = 1, Field2 = 10, Stringfield = "Object2" },
                           new TestObject { Field1 = 1, Field2 = 10, Stringfield = "Object3" },
                           new TestObject { Field1 = 1, Field2 = 10, Stringfield = "Object4" },
                           new TestObject { Field1 = 1, Field2 = 10, Stringfield = "Object5" }
                       };

        var subtotal = 0m;
        list.ForEach(x => x.SubTotalField = subtotal += x.Field1);

        Console.WriteLine("Object\tF1\tF2\tTotal");

        foreach (var testObject in list)
        {
            Console.WriteLine("{0}\t{1}\t{2}\t{3}", testObject.Stringfield, testObject.Field1, testObject.Field2, testObject.SubTotalField);
        }