我想将SQL Server语句更改为linq。我是linq的初学者。我需要在我的应用程序中修改linq语句,但我理解SQL语句。
这是我的SQL Server语句(结果对我来说没问题)。 我想将此SQL语句更改为linq语句。
SELECT
oh.OrderNo,
oh.PONo,
oh.CostCenter,
oh.ExpectedDeliveryDate,
do.DeliveryDate,
oh.PrimaryContactPerson,
oh.AlternateContactPerson,
oh.PrimaryContactNo,
oh.AlternateContactNo,
oh.CreatedDate,
oh.CreatedDate,
oh.WaybillNo,
do.DeliveryStatus,
oh.Email,
oh.Address,
oh.PostalCode,
oh.City,
oh.Branch,
oh.DONO,
ih.InvoiceNo,
ih.IsPaid
FROM dbo.tbl_OrderHeader AS oh
LEFT JOIN dbo.tbl_DeliveryOrder AS do ON oh.ID = do.OrderHeaderID
LEFT JOIN dbo.tbl_InvoiceHeader AS ih ON oh.ID = ih.OrderHeaderID
LEFT JOIN Admin.tbl_UserAccount AS ua ON oh.CreatedUserID = ua.ID
LEFT JOIN Admin.tbl_UserProfile AS up ON ua.UserProfileID = up.ID
WHERE oh.Active = 1 AND up.CompanyID = '05ba4b10-79d1-4367-a5ed-077be72dae86'
ORDER BY oh.OrderNo DESC
这需要修改我的linq语句。 (结果与sql语句不一样)
public IList<OHList> GetValues_Admin(Guid companyID)
{
IList<OHList> tblObj = null;
try
{
tblObj = (from OH in _entities.tbl_OrderHeader
join ua in _entities.tbl_UserAccount on OH.CreatedUserID equals ua.ID
join up in _entities.tbl_UserProfile on ua.UserProfileID equals up.ID
join IH in _entities.tbl_InvoiceHeader on OH.ID equals IH.OrderHeaderID
join DO in _entities.tbl_DeliveryOrder on OH.ID equals DO.OrderHeaderID
into O
from DO in O.DefaultIfEmpty()
where (OH.Active == true) && (up.CompanyID == companyID)
orderby OH.OrderNo descending
select new OHList
{
OrderNo = OH.OrderNo,
PONo = OH.PONo,
CostCenter = OH.CostCenter,
ExpectedDeliveryDate = OH.ExpectedDeliveryDate,
DeliveryDate = DO.DeliveryDate,
PrimaryContactPerson = OH.PrimaryContactPerson ?? "",
AlternateContactPerson = OH.AlternateContactPerson ?? "",
PrimaryContactNo = OH.AlternateContactNo ?? "",
AlternateContactNo = OH.AlternateContactNo ?? "",
CreatedDate = OH.CreatedDate,
OrderDate = OH.CreatedDate,
WaybillNo = OH.WaybillNo,
DeliveryStatus = DO.DeliveryStatus,
Email = OH.Email ?? "",
Address = OH.Address ?? "",
PostalCode = OH.PostalCode ?? "",
City = OH.City ?? "",
Branch = OH.Branch,
DONo = DO.DONo,
InvoiceNo = IH.InvoiceNo ?? "",
IsPaid = IH.IsPaid
}).ToList();
}
catch (Exception ex)
{
base.Exception(ex);
}
return tblObj;
}
答案 0 :(得分:0)
我用这段代码转换成了Linq。
感谢所有建议。
参考this link。
public IList<OHList> GetValues_Admin(Guid companyID)
{
IList<OHList> tblObj = null;
try
{
tblObj = (from OH in _entities.tbl_OrderHeader
join DO in _entities.tbl_DeliveryOrder on OH.ID equals DO.OrderHeaderID into t1
from t11 in t1.DefaultIfEmpty()
join IH in _entities.tbl_InvoiceHeader on OH.ID equals IH.OrderHeaderID into t2
from t22 in t2.DefaultIfEmpty()
join UA in _entities.tbl_UserAccount on OH.CreatedUserID equals UA.ID
join UP in _entities.tbl_UserProfile on UA.UserProfileID equals UP.ID
where (OH.Active == true) && (UP.CompanyID == companyID)
orderby OH.OrderNo descending
select new OHList
{
OrderNo = OH.OrderNo,
PONo = OH.PONo,
CostCenter = OH.CostCenter,
ExpectedDeliveryDate = OH.ExpectedDeliveryDate,
DeliveryDate = t11.DeliveryDate,
PrimaryContactPerson = OH.PrimaryContactPerson ?? "",
AlternateContactPerson = OH.AlternateContactPerson ?? "",
PrimaryContactNo = OH.AlternateContactNo ?? "",
AlternateContactNo = OH.AlternateContactNo ?? "",
CreatedDate = OH.CreatedDate,
OrderDate = OH.CreatedDate,
WaybillNo = OH.WaybillNo,
DeliveryStatus = t11.DeliveryStatus,
Email = OH.Email ?? "",
Address = OH.Address ?? "",
PostalCode = OH.PostalCode ?? "",
City = OH.City ?? "",
Branch = OH.Branch,
DONo = t11.DONo,
InvoiceNo = t22.InvoiceNo ?? "",
IsPaid = (t22.IsPaid == true) ? true : (bool?)null
}).ToList();
}
catch (Exception ex)
{
base.Exception(ex);
}
return tblObj;
}