2015-04-04 49 views
0

我有這個表中的SQL和使用vb.net:如何總結此前的紀錄與LINQ和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() 

但它不起作用。

+0

「它不工作」不適合我。 – 2015-04-04 13:05:03

+0

@ SamAxe:你能幫我嗎? – 2015-04-04 13:07:19

回答

2

雖然我最初認爲它不應該去(簡單地說,因爲你的總和是基於它之前的記錄),但是有一種方法可以繞過它,只需使用返回當前總和的函數並增加一個通過每個新的選擇變量。

不過,我仍然認爲,這應該只計算一次定義的順序,因爲如果用戶想通過名稱或金錢秩序,數額會有所不同,以及

但這裏是一個這樣,如果你願意,你可以做到這一點(我的模擬數據,所以你可能需要做一個小的重構)在C#

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 

它甚至更容易一些,那裏你可以簡單地增加局部變量和結果然後加到總和上

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(); 
} 
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); 
     }