English 中文(简体)
C# - 消费总量,使用LINQ
原标题:C# -Datewise total and GrandTotal using LINQ
  • 时间:2009-12-09 12:33:48
  •  标签:
  • c#
  • linq

From the Data,I have to find datewise total and GrandTotal the appropriate row should be filled with (***) when not applicable).

OrderID     OrderAmt   OrderDate                                              
----------- ---------- --------------------
1           10.50      2003-10-11 08:00:00
2           11.50      2003-10-11 10:00:00
3           1.25       2003-10-11 12:00:00
4           100.57     2003-10-12 09:00:00
5           19.99      2003-10-12 11:00:00
6           47.14      2003-10-13 10:00:00
7           10.08      2003-10-13 12:00:00
8           7.50       2003-10-13 19:00:00
9           9.50       2003-10-13 21:00:00

<><>Output

OrderID     Order Date OrderAmt   Sub Total  Grand Total 
----------- ---------- ---------- ---------- ----------- 
1           10/11/2003 10.50       ***         ***            
2           10/11/2003 11.50       ***         ***           
3           10/11/2003 1.25       23.25        ***        
4           10/12/2003 100.57      ***         ***            
5           10/12/2003 19.99      120.56       ***        
6           10/13/2003 47.14      ***          ***           
7           10/13/2003 10.08      ***          ***           
8           10/13/2003 7.50       ***          ***           
9           10/13/2003 9.50       74.22      218.03

The What are the change required in the following query ?

var qry =
                (from o in ord

                 select new
                 {

                     OrdNumber = o.OrderNumber,
                     OrdDate = o.OrderDate,
                     Amount = o.OrderAmount,
                     GrandTotal = ord.Sum(p => p.OrderAmount)
                 }
                  ).ToList();
最佳回答

除了不可读和极低的效率外,这应当做到:

var SubTotals = from o in ord
                group o by o.OrderDate.Date into g
                    select new
                               {
                                   Id = g.Max(x => x.OrderNumber),
                                   Amount = g.Sum(x => x.OrderAmount)
                               };
var qry = from o in ord
          orderby o.OrderDate
          let subTotals = SubTotals.Where(x => x.Id == o.OrderNumber)
          let grandTotal = ord.Sum(x => x.OrderAmount)
          let lastId = ord.OrderBy(x => x.OrderNumber).Last().OrderNumber
          select
              new
                  {
                      OrderNumber = o.OrderNumber,
                      OrderDate = o.OrderDate.Date,
                      Amount = o.OrderAmount,
                      SubTotal = (subTotals.Any()
                                    ? subTotals.First().Amount.ToString()
                                    : "***"),
                      GrandTotal = (o.OrderNumber == lastId
                                    ? grandTotal.ToString()
                                    : "***")
                  };

作为一般否认者,我敦促你重新考虑使用林克来编排数据。 如果你只与使用挂钩,但鉴于机会,在仅仅 lo取数据集时,没有羞耻,那么打上这种编号就是罚款的。

问题回答

这将按数据分类,并显示清单中的总和。

public class MyClass
{
    public int OrderID;
    public decimal OrderAmount;
    public DateTime OrderDate;
}

List<MyClass> list = new List<MyClass>();
list.Add(new MyClass { OrderID = 1, OrderAmount = 10.50m, OrderDate = new DateTime(2009, 10, 11, 8, 0, 0) });
list.Add(new MyClass { OrderID = 1, OrderAmount = 11.50m, OrderDate = new DateTime(2009, 10, 11, 10, 0, 0) });
list.Add(new MyClass { OrderID = 1, OrderAmount = 1.25m, OrderDate = new DateTime(2009, 10, 11, 12, 0, 0) });

list.Add(new MyClass { OrderID = 1, OrderAmount = 100.57m, OrderDate = new DateTime(2009, 10, 12, 09, 0, 0) });
list.Add(new MyClass { OrderID = 1, OrderAmount = 19.99m, OrderDate = new DateTime(2009, 10, 12, 11, 0, 0) });

var t = from l in list
    group l by l.OrderDate.Date into g
    let sum = g.Sum(x => x.OrderAmount)
    select new { g.Key, sum , GrandTotal = list.Sum(x => x.OrderAmount) };

你们是否想把它纳入上述要求的格式?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签