SELECT DISTINCT SOM.CompanyID,
SOM.BranchID,
SOM.SOID,
SOM.TranDocNo,
SOM.DocDate,
CM.CustomerName,
STM.SaleTypeName,
SOM.NetAmount,
SOM.TotQty
FROM tblSODetails SOD WITH (NOLOCK)
INNER JOIN tblSOMaster SOM WITH (NOLOCK) ON (SOD.SOID = SOM.SOID)
INNER JOIN tblSaleTypeMaster STM WITH (NOLOCK) ON (SOM.SaleTypeID = STM.SaleTypeID)
INNER JOIN tblCustomerMaster CM WITH (NOLOCK) on (SOM.CustomerID = CM.CustomerID)
LEFT OUTER JOIN tblSOReturnDetails SORD WITH (NOLOCK) ON (SOD.SODetID = SORD.SODetID)
LEFT OUTER JOIN tblSOReturnMaster SORM WITH (NOLOCK) ON (SORD.SOReturnID = SORM.SOReturnID)
LEFT OUTER JOIN tblOutwardMaster OM WITH (NOLOCK) ON (SOM.TranDocNo = OM.RefTranDocNo)
LEFT OUTER JOIN tblOutwardDetails OD WITH (NOLOCK) ON (OM.OutwardID = OD.OutwardID AND OD.ItemID = SOD.ItemID)
WHERE SOM.CompanyID = 1
and SOM.BranchID = 1
and SOM.IsOutward = 0
and SOM.DocTypeID = 3
and SOM.IsSOReturn = 0
and SOM.IsDealer = 0
and STM.IsStockEffect = 1
and STM.IsSaleOrder = 1
and SOM.Status = 'AP'
and CM.IsBlackListed = 0
AND SOD.Qty > (ISNULL(OD.Qty,0) + ISNULL(SORD.Qty,0))
如何以LINQ查询的形式编写此SQL查询.. 并且具体指定查询的最后一行。在LINQ查询中写入
我写这个查询来解决我的问题。
(from SOD in db.tblSODetails
join SOM in db.tblSOMasters on SOD.SOID equals SOM.SOID
join STM in db.tblSaleTypeMasters on SOM.SaleTypeID equals STM.SaleTypeID
SOM.CustomerID等于CM.CustomerID 在SOD.SODetID上的db.tblSOReturnDetails中加入SORD,将SORD.SODetID等于SORD_join
from SORD in SORD_join.DefaultIfEmpty()
from CM in db.tblCustomerMasters.Where(
CM => CM.CustomerID == SOM.CustomerID && (CM.IsBlackListed == false))
SOD.SODetID).DefaultIfEmpty()
join SORM in db.tblSOReturnMasters on SORD.SOReturnID equals SORM.SOReturnID into SORM_join
from SORM in SORM_join.DefaultIfEmpty()
join OM in db.tblOutwardMasters on new { TranDocNo = SOM.TranDocNo } equals
new { TranDocNo = OM.RefTranDocNo } into OM_join
from OM in OM_join.DefaultIfEmpty()
join OD in db.tblOutwardDetails on new { OM.OutwardID, SOD.ItemID } equals
new { OD.OutwardID, OD.ItemID } into OD_join
from OD in OD_join.DefaultIfEmpty()
where (SOM.CompanyID == compID)
&& (SOM.BranchID == BranchID)
&& ((isOutward == -1) || (SOM.IsOutward == (isOutward == 1 ? true : false)))
&& (SOM.DocTypeID == 3)
&& (SOM.Status == "AP")
&& (SOM.IsSOReturn == false)
&& (SOM.IsDealer == false)
&& (STM.IsStockEffect == true)
&& (STM.IsSaleOrder == true)
&& ((SOM.TranDocNo.Contains(strDocTypeCode)))
&& SOD.Qty > (((System.Decimal?)(OM.TotQty) ?? (System.Decimal?)0) + ((System.Decimal?)(SORM.TotQty) ?? (System.Decimal?)0))
select new
{
SOM.CompanyID,
SOM.BranchID,
SOM.SOID,
TranDocNo = SOM.TranDocNo,
DocDate = SOM.DocDate,
CustomerName = CM.CustomerName == null ? "" : CM.CustomerName,
DealerName = "",
STM.SaleTypeName,
NetAmount = SOM.NetAmount,
TotQty = SOM.TotQty
}
).Distinct();
答案 0 :(得分:1)
LINQ中有流畅的查询表达式样式 如果您希望您的项目数据访问使用LINQ。然后,您需要从其中一个LINQ样式开始编写查询。 我不建议编写SQL查询然后转换它,你会浪费很多时间来做这件事。一旦你熟悉并熟悉LINQ,你会发现使用LINQ而不是转换来开始查询要容易得多。
在JOINING表中使用查询表达式对于初学者来说更容易,而只需从一个表中查找数据 Fluent 样式就更容易了。
从查询风格
开始from thing1 in table1 join thing2 in table2 on thing1.Id equals thing2.Thing1Id
select new // anonymous type
{
Thing1Column= thing1.SomeColumn,
Thing2Comumn= thing2.SomeColumn,
BlaBlaBla= thingX.blablabla
};
或者您可以使用流畅的风格
Join运算符采用了许多参数:
Table1.Join(Table2,(Thing1 outerKey)=> outerKey.Id,(Thing2 innerKey)=> innerKey.Thing1Id,(thing1,thing2)=> thing1.AnyColumn +" - " + thing2.AnyOtherColumn);
Thing1是班级(型号) thing1是该类的对象
对于左外部的内部,那么你需要 DefaultIfEmpty() 它为您提供第二个表中没有匹配元素的结果
from thing1 in table1 join thing2 in table2 on thing1.Id equals thing2.Thing1Id into thingies2Group from x in reviewGroup.DefaultIfEmpty(new Thing2{MayBeEmptyValuesColumn= "DefaultValueIfEmpty"})
select new // anonymous type
{
Thing1Column= thing1.SomeColumn,
Thing2Comumn= x.MayBeEmptyValuesColumn,
BlaBlaBla= thingX.blablabla
};