需要与SQL查询等效的LINQ
我需要linq等价于下面的sql查询。Linq返回的记录比sql查询多。谁能帮我修一下linq吗需要与SQL查询等效的LINQ,sql,linq,Sql,Linq,我需要linq等价于下面的sql查询。Linq返回的记录比sql查询多。谁能帮我修一下linq吗 SELECT DISTINCT a.productname, u.areaonename,u.areatwoname, u.areathreename,u.areafourname, u.areafivename,a.agentproductid,tt.triptypename FROM Underwriter AS u INNER JOIN Rateset AS r ON u.Underwr
SELECT DISTINCT a.productname, u.areaonename,u.areatwoname, u.areathreename,u.areafourname, u.areafivename,a.agentproductid,tt.triptypename
FROM Underwriter AS u
INNER JOIN Rateset AS r ON u.Underwriterid = r.underwriterid
INNER JOIN BaseProduct b ON b.ratesetid = r.ratesetid
INNER JOIN AgentProduct a ON a.baseproductid = b.baseproductid
INNER JOIN AgentBranding ab ON a.agentproductid in( ab.productoneid, ab.producttwoid,
ab.productthreeid, ab.productfourid,ab.productfiveid)
INNER JOIN Agent ag ON ag.defaultagentbrandingid = ab.agentbrandingid
INNER JOIN triptypelink tl on tl.linkid in( a.agentproductid, a.agentproductid,
a.agentproductid,a.agentproductid, a.agentproductid)
INNER JOIN triptype tt on tl.triptypeid = tt.triptypeid
WHERE ag.agentid = 1234
(from un in db.Underwriters
join r in db.Ratesets on un.UnderwriterId equals r.UnderwriterId
join bs in db.BaseProducts on r.RatesetId equals bs.RatesetId
join ap in db.AgentProducts on bs.BaseProductId equals
ap.BaseProductId
from ag in db.AgentBrandings.Where(x => ap.AgentProductId ==
x.ProductOneId || ap.AgentProductId == x.ProductTwoId ||
ap.AgentProductId == x.ProductThreeId || ap.AgentProductId ==
x.ProductFourId || ap.AgentProductId == x.ProductFiveId)
join at in db.Agents on ag.AgentId equals at.AgentId
from ttl in db.TripTypeLinks where where (ag.ProductOneId ==
ttl.LinkId || ag.ProductTwoId == ttl.LinkId ||
ag.ProductThreeId == ttl.LinkId || ag.ProductFourId == ttl.LinkId ||
ag.ProductFiveId == ttl.LinkId)
join tt in db.TripTypes on ttl.TripTypeId equals tt.TripTypeId
where at.AgentId.Equals(agentId)
select new AgentModel
{
AreaOne = un.AreaOneName,
AreaTwo = un.AreaTwoName,
AreaThree = un.AreaThreeName,
AreaFour = un.AreaFourName,
AreaFive = un.AreaFiveName,
ProductId = ap.AgentProductId,
ProductName = ap.ProductName,
TripTypeName = tt.TripTypeName,
TripTypeCode = tt.TripTypeCode,
TripTypeId = tt.TripTypeId
}
)
.Distinct().ToListAsync<AgentModel>();`
这是我试过的
(from un in db.Underwriters
join r in db.Ratesets on un.UnderwriterId equals r.UnderwriterId
join bs in db.BaseProducts on r.RatesetId equals bs.RatesetId
join ap in db.AgentProducts on bs.BaseProductId equals
ap.BaseProductId
from ag in db.AgentBrandings.Where(x => ap.AgentProductId ==
x.ProductOneId || ap.AgentProductId == x.ProductTwoId ||
ap.AgentProductId == x.ProductThreeId || ap.AgentProductId ==
x.ProductFourId || ap.AgentProductId == x.ProductFiveId)
join at in db.Agents on ag.AgentId equals at.AgentId
from ttl in db.TripTypeLinks where where (ag.ProductOneId ==
ttl.LinkId || ag.ProductTwoId == ttl.LinkId ||
ag.ProductThreeId == ttl.LinkId || ag.ProductFourId == ttl.LinkId ||
ag.ProductFiveId == ttl.LinkId)
join tt in db.TripTypes on ttl.TripTypeId equals tt.TripTypeId
where at.AgentId.Equals(agentId)
select new AgentModel
{
AreaOne = un.AreaOneName,
AreaTwo = un.AreaTwoName,
AreaThree = un.AreaThreeName,
AreaFour = un.AreaFourName,
AreaFive = un.AreaFiveName,
ProductId = ap.AgentProductId,
ProductName = ap.ProductName,
TripTypeName = tt.TripTypeName,
TripTypeCode = tt.TripTypeCode,
TripTypeId = tt.TripTypeId
}
)
.Distinct().ToListAsync<AgentModel>();`
`
(from un in db.Underwriters
join r in db.Ratesets on un.UnderwriterId equals r.UnderwriterId
join bs in db.BaseProducts on r.RatesetId equals bs.RatesetId
join ap in db.AgentProducts on bs.BaseProductId equals
ap.BaseProductId
from ag in db.AgentBrandings.Where(x => ap.AgentProductId ==
x.ProductOneId || ap.AgentProductId == x.ProductTwoId ||
ap.AgentProductId == x.ProductThreeId || ap.AgentProductId ==
x.ProductFourId || ap.AgentProductId == x.ProductFiveId)
join at in db.Agents on ag.AgentId equals at.AgentId
from ttl in db.TripTypeLinks where where (ag.ProductOneId ==
ttl.LinkId || ag.ProductTwoId == ttl.LinkId ||
ag.ProductThreeId == ttl.LinkId || ag.ProductFourId == ttl.LinkId ||
ag.ProductFiveId == ttl.LinkId)
join tt in db.TripTypes on ttl.TripTypeId equals tt.TripTypeId
where at.AgentId.Equals(agentId)
select new AgentModel
{
AreaOne = un.AreaOneName,
AreaTwo = un.AreaTwoName,
AreaThree = un.AreaThreeName,
AreaFour = un.AreaFourName,
AreaFive = un.AreaFiveName,
ProductId = ap.AgentProductId,
ProductName = ap.ProductName,
TripTypeName = tt.TripTypeName,
TripTypeCode = tt.TripTypeCode,
TripTypeId = tt.TripTypeId
}
)
.Distinct().ToListAsync<AgentModel>();`
(来自联合国db.保险商
将r加入联合国保险商ID上的db.费率集等于r.保险商ID
将bs加入r.RatesetId等于bs.RatesetId的db.BaseProducts中
将ap加入到bs.BaseProductId等于的db.AgentProducts中
ap.BaseProductId
从db.AgentBrandings.Where(x=>ap.AgentProductId==
x、 ProductOneId | | ap.AgentProductId==x.ProductTwoId||
ap.AgentProductId==x.ProductThreeId | | ap.AgentProductId==
x、 ProductFourId | | ap.AgentProductId==x.ProductFiveId)
在ag.AgentId上的db.Agents中加入at等于at.AgentId
来自db.TripTypeLinks中的ttl,其中(ag.ProductOneId==
ttl.LinkId | | ag.ProductTwoId==ttl.LinkId||
ag.ProductThreeId==ttl.LinkId | | ag.ProductFourId==ttl.LinkId | |
ag.ProductFiveId==ttl.LinkId)
在ttl上的db.TripTypes中加入tt。TripTypeId等于tt.TripTypeId
其中at.AgentId等于(AgentId)
选择new AgentModel
{
AreaOne=un.AreaOneName,
AreaTwo=un.AreaTwoName,
AreaThree=un.AreaThreeName,
AreaFour=un.AreaFourName,
AreaFive=un.AreaFiveName,
ProductId=ap.AgentProductId,
ProductName=ap.ProductName,
TripTypeName=tt.TripTypeName,
TripTypeCode=tt.TripTypeCode,
TripTypeId=tt.TripTypeId
}
)
.Distinct().ToListAsync()`
谢谢你的帮助 我建议将连接替换为简单的from语句。对于远程数据源,联接通常执行得不好
(from un in db.Underwriters
join r in db.Ratesets on un.UnderwriterId equals r.UnderwriterId
join bs in db.BaseProducts on r.RatesetId equals bs.RatesetId
join ap in db.AgentProducts on bs.BaseProductId equals
ap.BaseProductId
from ag in db.AgentBrandings.Where(x => ap.AgentProductId ==
x.ProductOneId || ap.AgentProductId == x.ProductTwoId ||
ap.AgentProductId == x.ProductThreeId || ap.AgentProductId ==
x.ProductFourId || ap.AgentProductId == x.ProductFiveId)
join at in db.Agents on ag.AgentId equals at.AgentId
from ttl in db.TripTypeLinks where where (ag.ProductOneId ==
ttl.LinkId || ag.ProductTwoId == ttl.LinkId ||
ag.ProductThreeId == ttl.LinkId || ag.ProductFourId == ttl.LinkId ||
ag.ProductFiveId == ttl.LinkId)
join tt in db.TripTypes on ttl.TripTypeId equals tt.TripTypeId
where at.AgentId.Equals(agentId)
select new AgentModel
{
AreaOne = un.AreaOneName,
AreaTwo = un.AreaTwoName,
AreaThree = un.AreaThreeName,
AreaFour = un.AreaFourName,
AreaFive = un.AreaFiveName,
ProductId = ap.AgentProductId,
ProductName = ap.ProductName,
TripTypeName = tt.TripTypeName,
TripTypeCode = tt.TripTypeCode,
TripTypeId = tt.TripTypeId
}
)
.Distinct().ToListAsync<AgentModel>();`
因此,您的查询如下所示:
(from un in db.Underwriters
join r in db.Ratesets on un.UnderwriterId equals r.UnderwriterId
join bs in db.BaseProducts on r.RatesetId equals bs.RatesetId
join ap in db.AgentProducts on bs.BaseProductId equals
ap.BaseProductId
from ag in db.AgentBrandings.Where(x => ap.AgentProductId ==
x.ProductOneId || ap.AgentProductId == x.ProductTwoId ||
ap.AgentProductId == x.ProductThreeId || ap.AgentProductId ==
x.ProductFourId || ap.AgentProductId == x.ProductFiveId)
join at in db.Agents on ag.AgentId equals at.AgentId
from ttl in db.TripTypeLinks where where (ag.ProductOneId ==
ttl.LinkId || ag.ProductTwoId == ttl.LinkId ||
ag.ProductThreeId == ttl.LinkId || ag.ProductFourId == ttl.LinkId ||
ag.ProductFiveId == ttl.LinkId)
join tt in db.TripTypes on ttl.TripTypeId equals tt.TripTypeId
where at.AgentId.Equals(agentId)
select new AgentModel
{
AreaOne = un.AreaOneName,
AreaTwo = un.AreaTwoName,
AreaThree = un.AreaThreeName,
AreaFour = un.AreaFourName,
AreaFive = un.AreaFiveName,
ProductId = ap.AgentProductId,
ProductName = ap.ProductName,
TripTypeName = tt.TripTypeName,
TripTypeCode = tt.TripTypeCode,
TripTypeId = tt.TripTypeId
}
)
.Distinct().ToListAsync<AgentModel>();`
from un in db.Underwriters
from r in db.Ratesets
from bs in db.BaseProducts
from ap in db.AgentProducts
from ag in db.AgentBrandings
from at in db.Agents
from ttl in db.TripTypeLinks
from tt in db.TripTypes
where un.UnderwriterId == r.UnderwriterId
where r.RatesetId == bs.RatesetId
where bs.BaseProductId == ap.BaseProductId
Where ap.AgentProductId == x.ProductOneId
|| ap.AgentProductId == x.ProductTwoId
|| ap.AgentProductId == x.ProductThreeId
|| ap.AgentProductId == x.ProductFourId
|| ap.AgentProductId == x.ProductFiveId
where ag.AgentId equals at.AgentId
where ag.ProductOneId == ttl.LinkId
|| ag.ProductTwoId == ttl.LinkId
|| ag.ProductThreeId == ttl.LinkId
|| ag.ProductFourId == ttl.LinkId
|| ag.ProductFiveId == ttl.LinkId
where ttl.TripTypeId == tt.TripTypeId
where at.AgentId.Equals(agentId)
select new AgentModel
{
AreaOne = un.AreaOneName,
AreaTwo = un.AreaTwoName,
AreaThree = un.AreaThreeName,
AreaFour = un.AreaFourName,
AreaFive = un.AreaFiveName,
ProductId = ap.AgentProductId,
ProductName = ap.ProductName,
TripTypeName = tt.TripTypeName,
TripTypeCode = tt.TripTypeCode,
TripTypeId = tt.TripTypeId
}
)
.Distinct().ToListAsync<AgentModel>();
来自联合国银行承销商
从r开始,以db.Ratesets为单位
来自db.BaseProducts中的bs
来自db.AgentProducts中的ap
在db.AgentBrandings中来自ag
来自数据库代理中的at
从db.TripTypeLinks中的ttl
从tt到db.TripTypes
其中un.UNSERVIDER ID==r.UNSERVIDER ID
其中r.RatesetId==bs.RatesetId
其中bs.BaseProductId==ap.BaseProductId
其中ap.AgentProductId==x.ProductOneId
||ap.AgentProductId==x.PRODUCTWOID
||ap.AgentProductId==x.product3id
||ap.AgentProductId==x.ProductFourId
||ap.AgentProductId==x.ProductFiveId
其中ag.AgentId等于at.AgentId
其中ag.ProductOneId==ttl.LinkId
||ag.ProductTwoId==ttl.LinkId
||ag.ProductThreeId==ttl.LinkId
||ag.ProductFourId==ttl.LinkId
||ag.ProductFiveId==ttl.LinkId
其中ttl.TripTypeId==tt.TripTypeId
其中at.AgentId等于(AgentId)
选择new AgentModel
{
AreaOne=un.AreaOneName,
AreaTwo=un.AreaTwoName,
AreaThree=un.AreaThreeName,
AreaFour=un.AreaFourName,
AreaFive=un.AreaFiveName,
ProductId=ap.AgentProductId,
ProductName=ap.ProductName,
TripTypeName=tt.TripTypeName,
TripTypeCode=tt.TripTypeCode,
TripTypeId=tt.TripTypeId
}
)
.Distinct().ToListAsync();
我不确定它是否会工作,因为我不知道数据是如何构造的,但值得一试
(from un in db.Underwriters
join r in db.Ratesets on un.UnderwriterId equals r.UnderwriterId
join bs in db.BaseProducts on r.RatesetId equals bs.RatesetId
join ap in db.AgentProducts on bs.BaseProductId equals
ap.BaseProductId
from ag in db.AgentBrandings.Where(x => ap.AgentProductId ==
x.ProductOneId || ap.AgentProductId == x.ProductTwoId ||
ap.AgentProductId == x.ProductThreeId || ap.AgentProductId ==
x.ProductFourId || ap.AgentProductId == x.ProductFiveId)
join at in db.Agents on ag.AgentId equals at.AgentId
from ttl in db.TripTypeLinks where where (ag.ProductOneId ==
ttl.LinkId || ag.ProductTwoId == ttl.LinkId ||
ag.ProductThreeId == ttl.LinkId || ag.ProductFourId == ttl.LinkId ||
ag.ProductFiveId == ttl.LinkId)
join tt in db.TripTypes on ttl.TripTypeId equals tt.TripTypeId
where at.AgentId.Equals(agentId)
select new AgentModel
{
AreaOne = un.AreaOneName,
AreaTwo = un.AreaTwoName,
AreaThree = un.AreaThreeName,
AreaFour = un.AreaFourName,
AreaFive = un.AreaFiveName,
ProductId = ap.AgentProductId,
ProductName = ap.ProductName,
TripTypeName = tt.TripTypeName,
TripTypeCode = tt.TripTypeCode,
TripTypeId = tt.TripTypeId
}
)
.Distinct().ToListAsync<AgentModel>();`
如果仍然没有得到所需的结果,那么最好将查询分解成几个部分,首先得到所需的结果(因为在单个查询中有很多连接),然后尝试将其修补在一起
(from un in db.Underwriters
join r in db.Ratesets on un.UnderwriterId equals r.UnderwriterId
join bs in db.BaseProducts on r.RatesetId equals bs.RatesetId
join ap in db.AgentProducts on bs.BaseProductId equals
ap.BaseProductId
from ag in db.AgentBrandings.Where(x => ap.AgentProductId ==
x.ProductOneId || ap.AgentProductId == x.ProductTwoId ||
ap.AgentProductId == x.ProductThreeId || ap.AgentProductId ==
x.ProductFourId || ap.AgentProductId == x.ProductFiveId)
join at in db.Agents on ag.AgentId equals at.AgentId
from ttl in db.TripTypeLinks where where (ag.ProductOneId ==
ttl.LinkId || ag.ProductTwoId == ttl.LinkId ||
ag.ProductThreeId == ttl.LinkId || ag.ProductFourId == ttl.LinkId ||
ag.ProductFiveId == ttl.LinkId)
join tt in db.TripTypes on ttl.TripTypeId equals tt.TripTypeId
where at.AgentId.Equals(agentId)
select new AgentModel
{
AreaOne = un.AreaOneName,
AreaTwo = un.AreaTwoName,
AreaThree = un.AreaThreeName,
AreaFour = un.AreaFourName,
AreaFive = un.AreaFiveName,
ProductId = ap.AgentProductId,
ProductName = ap.ProductName,
TripTypeName = tt.TripTypeName,
TripTypeCode = tt.TripTypeCode,
TripTypeId = tt.TripTypeId
}
)
.Distinct().ToListAsync<AgentModel>();`
希望这有帮助。为什么要手动定义联接?它们是否已经不在您的数据模型中?假设数据模型有连接条件,如果没有连接条件,则无需定义任何连接条件-这是一个基本缺陷。请显示类模型,以便导航属性和关联的多样性可见。另外,告诉你的目标是什么类型的LINQ(对实体?)。还有,到底需要修复什么?不,删除连接是一个非常糟糕的主意。语句被转换为SQL,关系数据库对join语句进行了各种优化。您的语句连接没有后盾,但在远程数据源中通常执行得不好。
(from un in db.Underwriters
join r in db.Ratesets on un.UnderwriterId equals r.UnderwriterId
join bs in db.BaseProducts on r.RatesetId equals bs.RatesetId
join ap in db.AgentProducts on bs.BaseProductId equals
ap.BaseProductId
from ag in db.AgentBrandings.Where(x => ap.AgentProductId ==
x.ProductOneId || ap.AgentProductId == x.ProductTwoId ||
ap.AgentProductId == x.ProductThreeId || ap.AgentProductId ==
x.ProductFourId || ap.AgentProductId == x.ProductFiveId)
join at in db.Agents on ag.AgentId equals at.AgentId
from ttl in db.TripTypeLinks where where (ag.ProductOneId ==
ttl.LinkId || ag.ProductTwoId == ttl.LinkId ||
ag.ProductThreeId == ttl.LinkId || ag.ProductFourId == ttl.LinkId ||
ag.ProductFiveId == ttl.LinkId)
join tt in db.TripTypes on ttl.TripTypeId equals tt.TripTypeId
where at.AgentId.Equals(agentId)
select new AgentModel
{
AreaOne = un.AreaOneName,
AreaTwo = un.AreaTwoName,
AreaThree = un.AreaThreeName,
AreaFour = un.AreaFourName,
AreaFive = un.AreaFiveName,
ProductId = ap.AgentProductId,
ProductName = ap.ProductName,
TripTypeName = tt.TripTypeName,
TripTypeCode = tt.TripTypeCode,
TripTypeId = tt.TripTypeId
}
)
.Distinct().ToListAsync<AgentModel>();`