返回唯一值和Sum LINQ

时间:2016-11-23 12:00:19

标签: c# linq

我有两张桌子:

  1. 零售商
  2. 发票
  3. 零售商有两栏:

    1.1。 RetailerID

    1.2。 RetailerName

    发票有三栏:

    2.1。 InvoiceID

    2.2。 InvoiceProfit

    2.3。 RetailerID

    Retailers.RetailerIDInvoices.RetailerID(一对多)相关联。

    我想要做的是写一个linq(或以lambda exp的形式),返回Retailer.RetailerID,Retailer.RetailerName,Invoice.InvoiceProfit。

    我可以这样做:

    var retailers = from r in db.Retailers select t; 
    var invoices = from i in db.Invoices select i; 
    
    var retailersAndInvoices = from r in retailers join i in invoices on r.RetailerID equals i.RetailerID select new {t.RetailerName, i.InvoiceProfit}; 
    

    我想仅返回Distinct RetailerNamesSum所有Invoices.InvoiceProfit旁边的每个人 - 目的是“十大零售商”!

    我该怎么做?

2 个答案:

答案 0 :(得分:5)

  • 使用GroupByRetailerName
  • 将平面列表转换为群组
  • 使用Sum(i => i.InvoiceProfit)计算总计数
  • 使用new { ... }将零售商与其利润总额配对
  • 使用OrderByDescending(p => p.TotalProfit)将高利润零售商排在首位
  • 使用Take(10)将列表限制为十个项目。

整体查询如下所示:

var topTen = retailersAndInvoices
    .GroupBy(ri => ri.RetailerName)
    .Select(g => new {
        Retailer = g.Key
    ,   TotalProfit = g => g.Sum(i => i.InvoiceProfit)
    })
    .OrderByDescending(p => p.TotalProfit)
    .Take(10)
    .ToList();

答案 1 :(得分:0)

我使用lambda和linq的组合。请参见msdn:https://code.msdn.microsoft.com/LINQ-Join-Operators-dabef4e9

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication28
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable retailers = new DataTable();
            retailers.Columns.Add("RetailerID", typeof(int));
            retailers.Columns.Add("RetailerName", typeof(string));

            retailers.Rows.Add(new object[] { 123, "abc" });
            retailers.Rows.Add(new object[] { 124, "abd" });
            retailers.Rows.Add(new object[] { 125, "abe" });

            DataTable invoices = new DataTable();
            invoices.Columns.Add("InvoiceID", typeof(int));
            invoices.Columns.Add("InvoiceProfit", typeof(decimal));
            invoices.Columns.Add("RetailerID", typeof(int));

            invoices.Rows.Add(new object[] { 100, 200, 123 });
            invoices.Rows.Add(new object[] { 101, 201, 123 });
            invoices.Rows.Add(new object[] { 102, 202, 123 });
            invoices.Rows.Add(new object[] { 103, 203, 123 });
            invoices.Rows.Add(new object[] { 104, 204, 124 });
            invoices.Rows.Add(new object[] { 105, 205, 124 });
            invoices.Rows.Add(new object[] { 106, 206, 124 });
            invoices.Rows.Add(new object[] { 107, 207, 125 });
            invoices.Rows.Add(new object[] { 108, 208, 125 });
            invoices.Rows.Add(new object[] { 109, 209, 125 });

            var retailersAndInvoices = (from r in retailers.AsEnumerable()
                                        join i in invoices.AsEnumerable() on r.Field<int>("RetailerID") equals i.Field<int>("RetailerID")
                                        select new { name = r.Field<string>("RetailerName"), profit = i.Field<decimal>("InvoiceProfit") })
                                       .GroupBy(x => x.name)
                                       .Select(x => new { name = x.Key, totalProfit = x.Select(y => y.profit).Sum() }).ToList();


        }
    }
}