有个需求就是,我们要查询的信息分布在两个不同的数据库中,通过外键相互关联起来,然后返回datatable在前端展示内容。
根据需求我们可以考虑c#的linq 先在从不同的数据中获取相关的datatable,然后把两个datatable 通过 linq 关联查询返回内容
string sql = "select * from V_LoginLog where denglurq=@RQ";
DataTable dt = MsSqlHelper.ExecuteDataTable(ConnUeHis, sql, new SqlParameter("@RQ", rq));
string sql1 = "select * from app_getopdschdule where OPDDATE=:OPDDATE";
DataTable dt1 = OracleHelper.ExecuteDataTable(ConnStrLzHis, sql1, new OracleParameter(":OPDDATE", rq));
var res = from m in dt.AsEnumerable()
from s in dt1.AsEnumerable()
where m.Field<string>("LoginUser") == s.Field<string>("DOCTORID") && m.Field<string>("shangxiawbz") == Convert.ToString(s.Field<decimal>("OPDTIMEID"))
select new
{
DoctorName = s.Field<String>("DOCTORNAME"),
OpdTimeID = m.Field<String>("shangxiawbz"),
LoginTime = m.Field<DateTime>("denglusj"),
RoomName = s.Field<String>("ROOMNAME")
};
DataTable DtTemp = new DataTable();
DtTemp.Columns.Add("DoctorName", typeof(String));
DtTemp.Columns.Add("OpdTimeID", typeof(String));
DtTemp.Columns.Add("RoomName", typeof(String));
DtTemp.Columns.Add("LoginTime", typeof(String));
foreach (var obj in res)
{
DtTemp.Rows.Add(obj.DoctorName, obj.OpdTimeID == "" ? "上午" : "下午", obj.RoomName, obj.LoginTime.ToString("yyyy-MM-dd HH:mm:ss")); }
return CommonHelper.Dtb2Json(DtAll);