C# 实体框架Linq、左连接和具有总和和计数的组
我需要一些帮助来将SQL转换为Linq。在MySQL中它非常简单C# 实体框架Linq、左连接和具有总和和计数的组,c#,mysql,entity-framework,linq,entity,C#,Mysql,Entity Framework,Linq,Entity,我需要一些帮助来将SQL转换为Linq。在MySQL中它非常简单 Table: customers ID Name 1 Bill 2 John Table: purchases ID CustomerID CompletedTransaction 1 1 False 2 2 True 3 1 True 4 1 True SELECT c.ID c.
Table: customers
ID Name
1 Bill
2 John
Table: purchases
ID CustomerID CompletedTransaction
1 1 False
2 2 True
3 1 True
4 1 True
SELECT c.ID
c.Name,
COUNT(p.ID) AS TotalPurchases,
SUM(CASE WHEN p.CompletedTransaction = TRUE THEN 1 ELSE 0 END) AS TotalCompleted
FROM customers c
LEFT JOIN purchases p ON c.ID = p.CustomerID
GROUP BY c.ID
Expected Result:
1, Bill, 3, 2
2, John, 1, 1
我已经看到了一些关于如何在Linq中实现左连接的示例,但我不确定如何在其中包含求和和计数。我在Linq中看到过一些示例,其中返回的字段是从组键中选择的。这是否意味着如果我在customers表中有更多的字段,例如我想返回的地址和其他联系方式,我必须将它们包括在联接中,然后才能选择它们?希望这是有意义的。感谢任何可能为我指明正确方向的帮助或链接
谢谢这是样品
class Program
{
static void Main(string[] args)
{
List<Customers> customers = new List<Customers>();
customers.Add(new Customers() { ID = 1, Name = "Bill" });
customers.Add(new Customers() { ID = 2, Name = "John" });
List<Purchases> purchases = new List<Purchases>();
purchases.Add(new Purchases() { ID = 1, CustomerID = 1, CompletedTransaction = false });
purchases.Add(new Purchases() { ID = 2, CustomerID = 2, CompletedTransaction = true });
purchases.Add(new Purchases() { ID = 3, CustomerID = 1, CompletedTransaction = true });
purchases.Add(new Purchases() { ID = 4, CustomerID = 1, CompletedTransaction = true });
IEnumerable<JoinResult> results = from c in customers
join p in purchases
on c.ID equals p.CustomerID
group new { c, p } by new {p.CustomerID, c.Name} into r
select new JoinResult
{
CustomerID = r.Key.CustomerID,
CustomerName = r.Key.Name,
TotalPurchases = r.Count(),
TotalCompleteTransaction = r.Where(s=> s.p.CompletedTransaction).Count()
};
foreach(JoinResult r in results)
{
Console.WriteLine($"CustomerID : {r.CustomerID} | Name : {r.CustomerName} | TotalPurchases : {r.TotalPurchases} | TotalCompleteTransaction : {r.TotalCompleteTransaction}");
}
Console.ReadKey();
}
}
class Customers
{
public int ID { get; set; }
public string Name { get; set; }
}
class Purchases
{
public int ID { get; set; }
public int CustomerID { get; set; }
public bool CompletedTransaction { get; set; }
}
class JoinResult
{
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public int TotalPurchases { get; set; }
public int TotalCompleteTransaction { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
列出客户=新列表();
添加(新客户(){ID=1,Name=“Bill”});
添加(新客户(){ID=2,Name=“John”});
列表购买=新列表();
Add(new purchases(){ID=1,CustomerID=1,CompletedTransaction=false});
Add(new purchases(){ID=2,CustomerID=2,CompletedTransaction=true});
Add(new purchases(){ID=3,CustomerID=1,CompletedTransaction=true});
Add(new purchases(){ID=4,CustomerID=1,CompletedTransaction=true});
IEnumerable结果=来自客户中的c
参与采购
在c.ID上等于p.CustomerID
通过new{p.CustomerID,c.Name}将new{c,p}分组到r中
选择新结果
{
CustomerID=r.Key.CustomerID,
CustomerName=r.Key.Name,
TotalPurchases=r.Count(),
TotalCompleteTransaction=r.Where(s=>s.p.CompletedTransaction).Count()
};
foreach(结果中的JoinResult)
{
Console.WriteLine($“CustomerID:{r.CustomerID}名称:{r.CustomerName}总计采购:{r.TotalPurchases}总计采购:{r.TotalCompleteTransaction}”);
}
Console.ReadKey();
}
}
类客户
{
公共int ID{get;set;}
公共字符串名称{get;set;}
}
班级购买
{
公共int ID{get;set;}
public int CustomerID{get;set;}
public bool CompletedTransaction{get;set;}
}
类连接结果
{
public int CustomerID{get;set;}
公共字符串CustomerName{get;set;}
公共整数{get;set;}
公共int TotalCompleteTransaction{get;set;}
}
结果
感谢您的帮助,尽管看起来这是一个内部连接,而不是左连接。
var answer = (from c in db.customers
join p in db.purchases
on c.ID = p.CustomerID into subs
from sub in subs.DefaultIfEmpty()
group sub by new { c.ID, c.Name } into gr
select new {
gr.Key.ID,
gr.Key.Name,
Total = gr.Count(x => x != null),
CountCompleted = gr.Count(x => x != null && x.CompletedTransaction)
}).ToList();