我有一個包含工具和工人的匯總表。因此,我無法處理的任務是如何計算匯總表中未涉及的 lasts 工具的數量。我想它會是這樣的
var res = context.ToolsSummaryTable.Include(t => t.Tool).Include(t => t.Worker)
where(t.Tool.Name.Count() > t.Tool.Quantity);
請幫忙!
匯總表資料
工人ID | 工具ID |
---|---|
1 | 2 |
2 | 3 |
3 | 2 |
1 | 2 |
工具表資料
姓名 | 數量 |
---|---|
螺絲刀 | 2 |
悍馬 | 3 |
扳手 17 | 1 |
管鉗 | 2 |
工人表資料
名 | 姓 |
---|---|
安德魯 | 希臘人 |
戈登 | 僚機 |
山姆 | 維和人員 |
安東尼 | 偵察 |
uj5u.com熱心網友回復:
你需要一個左外連接。
左外連接是:“來自 g.DefaultIfEmpty() 中的 tuUsedNone”參見下面的代碼
using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Context context = new Context()
{
Sumary = new List<Summary>()
{
new Summary() {WorkerId = 1, ToolId = 2 },
new Summary() {WorkerId = 2, ToolId = 3 },
new Summary() {WorkerId = 3, ToolId = 2 },
new Summary() {WorkerId = 1, ToolId = 2 }
},
Tools = new List<Tools>()
{
new Tools() { ToolId = 1, Name = "Screwdriver", Quantity = 2},
new Tools() { ToolId = 2, Name = "Hummer", Quantity = 3},
new Tools() { ToolId = 3, Name = "Wrench 17", Quantity = 1},
new Tools() { ToolId = 4, Name = "Pipe Wrench", Quantity = 2}
},
Workers = new List<Workers>()
{
new Workers() { WorkerId = 1, FirstName = "Andrew", LastName = "Greekman"},
new Workers() { WorkerId = 2, FirstName = "Gordon", LastName = "Wingman"},
new Workers() { WorkerId = 3, FirstName = "Sam", LastName = "Peacekeeper"},
new Workers() { WorkerId = 4, FirstName = "Antony", LastName = "Scout"}
}
};
var toolsUsed = context.Sumary.GroupBy(x => x.ToolId)
.Select(x => new { toolId = x.Key, quantityUsed = x.Count() });
var toolsRemaining = (from t in context.Tools
join tu in toolsUsed on t.ToolId equals tu.toolId into g
from tuUsedNone in g.DefaultIfEmpty()
select (new { Name = t.Name, toolsRemaining = (tuUsedNone == null) ? t.Quantity : t.Quantity - tuUsedNone.quantityUsed }))
.ToList();
}
}
public class Context
{
public List<Summary> Sumary { get; set; }
public List<Tools> Tools { get; set; }
public List<Workers> Workers { get; set; }
}
public class Summary
{
public int WorkerId { get; set; }
public int ToolId { get; set; }
}
public class Tools
{
public int ToolId { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
}
public class Workers
{
public int WorkerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/493009.html
上一篇:基于另一個串列c#拆分一個串列