问题描述
朋友们,
我需要Linq查询才能从3个表中选择数据.
我用C#代码实现.但是我需要在LINQ Query中进行开发.
foreach(dtChapter.Rows中的DataRow drChapters)
{
DataRow dr = dtPlanInfo.NewRow();
foreach(dtMileStones.Rows中的DataRow drMileStone)
{
dr ["Chapter"] = drChapters ["Chapter"].ToString();
DataRow [] drStages = dtStages.Select("Chapter =""+ drChapters [" Chapter].ToString()+"''和MilestoneDesc =" + drMileStone ["MilestoneDesc"] +''");
foreach(drStages中的DataRow drs)
{
dr [drMileStone ["MilestoneDesc"].ToString()+"PlanDate"] = drs ["PlanDate"];
dr [drMileStone ["MilestoneDesc"].ToString()+"ActualDate"] = drs ["ActualDate"];
dr [drMileStone ["MilestoneDesc"].ToString()+"Remarks"] = drs ["Remarks"].ToString();
dr [drMileStone ["MilestoneDesc"].ToString()+"RevisedCount"] = drs ["RevisedCount"];
dr [drMileStone ["MilestoneDesc"].ToString()+"LastTimeRevised"] = drs ["LastRevisedPlanDate"];
}
}
dtPlanInfo.Rows.Add(dr);
}
涉及的表是
第 |
Ch1 |
Ch2 |
MileStone
MileStoneDesc |
M1 |
M2 |
StageId | PlanDate | ActualDate | 备注 | 第 | MilestoneDesc | revisedCount | LastTimeRevised |
1 | 12/9/2012 | 2012/12/15 | test | Ch1 | M1 | ||
2 | 3/2/2013 | 5/6/2013 | test2 | ch2 | M2 |
我需要结果为
章节 | M1Plan | M1Actual | M1Remark | M1RevisedCount | M1LastRevised | M2Plan | M2Actual | M2Remark | M2RevisedCount | M2LastRevised |
ch1 | 2012/12/9 | 2012/12/15 | test | |||||||
ch2 | 3/2/2013 | 5/6/2013 | test2 |
请提供Linq查询以获取此结果.
Hi friends,
i need the Linq Query to select datas from 3 Tables.
I acheived in c# Code. But i need to develop in LINQ Query.
foreach (DataRow drChapters in dtChapter.Rows)
{
DataRow dr = dtPlanInfo.NewRow();
foreach (DataRow drMileStone in dtMileStones.Rows)
{
dr["Chapter"] = drChapters["Chapter"].ToString();
DataRow[] drStages = dtStages.Select("Chapter=''" + drChapters["Chapter"].ToString() + "'' and MilestoneDesc=''" + drMileStone["MilestoneDesc"] + "''");
foreach (DataRow drs in drStages)
{
dr[drMileStone["MilestoneDesc"].ToString() + "PlanDate"] = drs["PlanDate"];
dr[drMileStone["MilestoneDesc"].ToString() + "ActualDate"] = drs["ActualDate"];
dr[drMileStone["MilestoneDesc"].ToString() + "Remarks"] = drs["Remarks"].ToString();
dr[drMileStone["MilestoneDesc"].ToString() + "RevisedCount"] = drs["RevisedCount"];
dr[drMileStone["MilestoneDesc"].ToString() + "LastTimeRevised"] = drs["LastRevisedPlanDate"];
}
}
dtPlanInfo.Rows.Add(dr);
}
The Tables involved are
Chapter |
Ch1 |
Ch2 |
MileStone
MileStoneDesc |
M1 |
M2 |
StageId | PlanDate | ActualDate | Remarks | Chapter | MilestoneDesc | revisedCount | LastTimeRevised |
1 | 12/9/2012 | 15/12/2012 | test | Ch1 | M1 | ||
2 | 3/2/2013 | 5/6/2013 | test2 | ch2 | M2 |
I need the Result as
Chapter | M1Plan | M1Actual | M1Remark | M1RevisedCount | M1LastRevised | M2Plan | M2Actual | M2Remark | M2RevisedCount | M2LastRevised |
ch1 | 12/9/2012 | 15/12/2012 | test | |||||||
ch2 | 3/2/2013 | 5/6/2013 | test2 |
Please provide me the Linq Query to get this result.
推荐答案
IEnumerable<dataset1.dtplaninforow> planInfos = DataSetInstance.dtStages.AsEnumerable()
.Where(st =>
DataSetInstance.dtChapter.Any(ch => ch.Chapter==st.Chapter) &&
DataSetInstance.dtMileStones.Any(ms => ms.MilestoneDesc==st.MilestoneDesc))
.Select(st => {
var dr = DataSetInstance.dtPlanInfo.NewdtPlanInfoRow();
dr.Chapter = st.Chapter;
dr[string.Format("{0}Plan", st.MilestoneDesc)] = st.PlanDate;
dr[string.Format("{0}Actual", st.MilestoneDesc)] = st.ActualDate;
dr[string.Format("{0}Remark", st.MilestoneDesc)] = st.Remarks;
dr[string.Format("{0}RevisedCount", st.MilestoneDesc)] = st.revisedCount;
dr[string.Format("{0}LastRevised", st.MilestoneDesc)] = st.LastTimeRevised;
return dr;
});
planInfos.CopyToDataTable(DataSetInstance.dtPlanInfo, LoadOption.PreserveChanges);
在PlanInfo
表的新行中设置值时,使用LINQ query expression syntax
很难获得上述结果.
It is somewhat difficult to get the above result using the LINQ query expression syntax
in setting the values in new row of PlanInfo
table.
这篇关于Linq进行多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!