我需要将下面的Sproc转换为Linq查询。在最底层是我到目前为止。作为参考,“splat”(不是我的sproc )背后的字段是
ImmunizationID int, HAReviewID int, ImmunizationMaintID int, ImmunizationOther varchar(50), ImmunizationDate smalldatetime, ImmunizationReasonID int
前两个分别是PK和FK。另外两个int
是维护表的链接,其中存储了描述。这就是我所坚持的,INNER JOIN
和LEFT OUTER JOIN
谢谢,
SELECT tblHAReviewImmunizations.*,
tblMaintItem.ItemDescription,
tblMaintItem2.ItemDescription as Reason
FROM
dbo.tblHAReviewImmunizations
INNER JOIN dbo.tblMaintItem
ON dbo.tblHAReviewImmunizations.ImmunizationMaintID =
dbo.tblMaintItem.ItemID
LEFT OUTER JOIN dbo.tblMaintItem as tblMaintItem2
ON dbo.tblHAReviewImmunizations.ImmunizationReasonID =
tblMaintItem2.ItemID
WHERE
HAReviewID = @haReviewID
到目前为止我的尝试 - >
public static DataTable GetImmunizations(int haReviewID)
{
using (var context = McpDataContext.Create())
{
var currentImmunizations =
from haReviewImmunization in context.tblHAReviewImmunizations
where haReviewImmunization.HAReviewID == haReviewID
join maintItem in context.tblMaintItems
on haReviewImmunization.ImmunizationReasonID
equals maintItem.ItemID into g
from maintItem in g.DefaultIfEmpty()
let Immunization = GetImmunizationNameByID(
haReviewImmunization.ImmunizationMaintID)
select new
{
haReviewImmunization.ImmunizationDate,
haReviewImmunization.ImmunizationOther,
Immunization,
Reason = maintItem == null ? " " : maintItem.ItemDescription
};
return currentImmunizations.CopyLinqToDataTable();
}
}
private static string GetImmunizationNameByID(int? immunizationID)
{
using (var context = McpDataContext.Create())
{
var domainName =
from maintItem in context.tblMaintItems
where maintItem.ItemID == immunizationID
select maintItem.ItemDescription;
return domainName.SingleOrDefault();
}
}
答案 0 :(得分:1)
public static DataTable GetImmunizations(int haReviewID)
{
using (var context = McpDataContext.Create())
{
var currentImmunizations =
from haReviewImmunization in context.tblHAReviewImmunizations
where haReviewImmunization.HAReviewID == haReviewID
join maintItem in context.tblMaintItems
on haReviewImmunization.ImmunizationMaintID
equals maintItem.ItemID
join maintItem2 in context.tblMaintItems
on haReviewImmunization.ImmunizationReasonID
equals maintItem2.ItemID into g
from maintItem3 in g.DefaultIfEmpty()
select new
{
haReviewImmunization.ImmunizationDate,
haReviewImmunization.ImmunizationOther,
maintItem.ItemDescription,
Reason = maintItem3 == null ? " " : maintItem3.ItemDescription
};
return currentImmunizations.CopyLinqToDataTable();
}
}