如何在LINQ中连接两个数据表并使用循环提取其值

时间:2013-08-05 11:13:58

标签: linq

我在两个数据表

中取得了价值

DT1

EMPID empname  sickleave  casual leave  earned leave
1      john                            1

Dt2

EmpId  empname  sickleave  casualleave  earned leave
 1      john      5          5           5

在dt2中给予假期的违约值(雇员的总休假)和雇员在dt1休假期间给出的假期。 所以我想以这种形式在网格中显示结果,如

Empname sickleave casualleave earnedleave
john       0/5       0/5          1/5

for this i wrote code like this

 var result = from p in dt1.AsEnumerable()
                                 join q in dt2.AsEnumerable() on p.Field<int>("EmpID") equals q.Field<int>("EmpID") into UP
                                 from q in UP.DefaultIfEmpty()

                                 select new
                                 {
                                     EmpID = p.Field<int>("EmpID"),
                                     EmpName = p.Field<string>("EmpName"),

                                     SickLeave = (p.Field<int?>("Sick Leave") ?? 0).ToString() + "/" + (q.Field<int?>("Sick Leave") ?? 0).ToString(),
                                     CasualLeave = (p.Field<int?>("Casual Leave") ?? 0).ToString() + "/" + (q.Field<int?>("Casual Leave") ?? 0).ToString(), 
                                     EarnedLeave = (p.Field<int?>("Earned Leave") ?? 0).ToString() + "/" + (q.Field<int?>("Earned Leave") ?? 0).ToString()
                                 };

                    GridViewLeaveSummary.DataSource = result;
                    GridViewLeaveSummary.DataBind();

这个代码工作正常但是我想让离开动态意味着网格显示没有离开根据离开类型表(意味着所有叶子字段来自datable)因为我必须添加不同的离开类型。在此代码olny三叶给出,我必须添加更多的叶子。在网格中没有叶子显示根据数据库

谢谢

0 个答案:

没有答案