2016-11-23 107 views
2

我有兩個表:返回唯一值和總LINQ

  1. 零售商
  2. 發票

零售商有兩列:

1.1。零售商ID

1.2。零售商名稱

發票有三列:

2.1。 InvoiceID

2.2。 InvoiceProfit

2.3。零售商ID

Retailers.RetailerID鏈接到Invoices.RetailerID(一對多)。

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

我能做到這一點,像這樣:

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 RetailerNames和所有Invoices.InvoiceProfit未來的Sum每一個 - 其目的是「十大零售商」!

我該怎麼做?

回答

4
  • 使用GroupBy通過RetailerName
  • 使用Sum(i => i.InvoiceProfit)轉換一個平面列表到組來計算總計
  • 使用new { ... }配對了零售商的利潤總額
  • 使用OrderByDescending(p => p.TotalProfit)獲得高利潤的零售商頂部
  • 使用Take(10)將列表限制爲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(); 
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(); 


     } 
    } 
}