现在已经绕了几个小时,似乎还不太正确。通过OleDB连接加载大型数据集,以便使用数据集和数据表
我要做的是获取Job.PeriodRequired分组的JobEstimate.Quantity的总和,其中Job.JobType =“ Q”,Job.JobGroupCode =“ GOLD”和JobEstimate.ProductCode =“ 10000”
到目前为止,我得到的最好的结果是下面的代码,但只给出了第一条估算行中的数量
var periodQuantity = from je in ds.JobEstimate
join j in ds.Job on je.JobCode equals j.JobCode
where je.ProductCode == "10000" &&
j.JobType == "Q" &&
j.JobGroupCode == "GOLD"
group je by j.PeriodRequired into g
select new { PeriodId = g.Key, Quantity = g.Sum(e => e.Quantity) };
答案 0 :(得分:1)
由于问题中的DataType不够清晰,我在这里尝试使用类来对其进行模拟。请更改代码以适合您的数据类型
public class Job
{
public string JobCode{get;set;}
public string JobName{get;set;}
public string PeriodRequired{get;set;}
public string JobType{get;set;}
public string JobGroupCode{get;set;}
public DateTime DateRequired{get;set;}
}
public class JobEstimate
{
public string JobCode{get;set;}
public string ProductCode{get;set;}
public int Qty {get;set;}
}
例如数据
var jobList = new List<Job>(){
new Job{JobCode="abc1",JobName="JobName1",PeriodRequired="1",JobType="Q",JobGroupCode="GOLD"},
new Job{JobCode="abc2",JobName="JobName1",PeriodRequired="2",JobType="Q",JobGroupCode="GOLD"},
new Job{JobCode="abc3",JobName="JobName1",PeriodRequired="3",JobType="R",JobGroupCode="GOLD"},
new Job{JobCode="abc4",JobName="JobName1",PeriodRequired="1",JobType="Q",JobGroupCode="GOLD"}
};
var jobEstimateList = new List<JobEstimate>(){
new JobEstimate(){JobCode="abc1",ProductCode="10000",Qty=12},
new JobEstimate(){JobCode="abc1",ProductCode="10000",Qty=11},
new JobEstimate(){JobCode="abc2",ProductCode="10000",Qty=12},
new JobEstimate(){JobCode="abc3",ProductCode="10000",Qty=14},
};
您可以获得所需的结果
var result = jobList.Join(jobEstimateList,
job=> job.JobCode,
je=>je.JobCode,(job,je)=>new {
JobCode=job.JobCode,
JobName = job.JobName,
JobType = job.JobType,
JobGroupCode = job.JobGroupCode,
ProductCode = je.ProductCode,
PeriodRequired = job.PeriodRequired,
Qty = je.Qty})
.Where(x=>x.JobType=="Q" && x.JobGroupCode=="GOLD" && x.ProductCode=="10000")
.GroupBy(x=> x.PeriodRequired)
.Select(x=> new {PeriodId = x.Key,Qty= x.Sum(c=>c.Qty)});
结果
PeriodId | Qty
1 | 23
2 | 12