我有一个下面的SQL查询,它有一些GroupBy,条件等更复杂,我在帖子中跳过这些让我的问题变得容易:
SELECT
SUBSTRING(TSL.ctg_name, 6, 100) AS 'TREKK', *
FROM Filteredctg_timbersettlementline AS TSL
INNER JOIN Filteredctg_timbersettlement AS TS
ON TSL.ctg_timbersettlementid = TS.ctg_timbersettlementid
LEFT JOIN FilteredNew_property AS P
ON TS.ctg_propertyid = P.new_propertyid
WHERE (TS.ctg_timbersettlementid = @TimberSettlementID) AND
(TSL.ctg_reportgroup = 'TREKK')
我在Linq下面尝试过:
var Trekks = (from ts in XrmContext.ctg_timbersettlementSet
join tsl in XrmContext.ctg_timbersettlementlineSet
on ts.Id equals tsl.ctg_timbersettlementid.Id
join p in XrmContext.New_propertySet
on ts.ctg_propertyid.Id equals p.New_propertyId
into temp
from p in temp.DefaultIfEmpty()
where ts.ctg_timbersettlementId == TimberSettlementGuid
where tsl.ctg_reportgroup == "TREKK"
select new
{
tsl.ctg_name,
ts.ctg_BasisAllocatedForestryFund,
}).ToList();
它给我一个错误: 方法'GroupJoin'不能遵循'Join'方法或不支持。尝试根据支持的方法编写查询,或在调用不支持的方法之前调用'AsEnumerable'或'ToList'方法。
我怎样才能获得LEFT JOIN?
答案 0 :(得分:0)
要实现左连接,您需要使用中间DefaultIfEmpty():
var query = from c in db.Customers
join o in db.Orders
on c.CustomerID equals o.CustomerID into sr
from x in sr.DefaultIfEmpty()
select new {
CustomerID= c.CustomerID, ContactName=c.ContactName,
OrderID = x.OrderID == null ? -1 : x.OrderID};