Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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



StageIdPlanDateActualDateRemarksChapterMilestoneDescrevisedCountLastTimeRevised
112/9/201215/12/2012testCh1M1
23/2/20135/6/2013test2ch2M2


I need the Result as

ChapterM1PlanM1ActualM1RemarkM1RevisedCountM1LastRevisedM2PlanM2ActualM2RemarkM2RevisedCountM2LastRevised
ch112/9/201215/12/2012test
ch23/2/20135/6/2013test2


Please provide me the Linq Query to get this result.
Posted

1 solution

The following LINQ query with Method (Fluent) syntax can be used to achieve the above result
C#
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);

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.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900