我对使用Sql 2005使用Distinct with Entity Framework有疑问。在这个例子中:
practitioners = from p in context.Practitioners
join pn in context.ProviderNetworks on
p.ProviderId equals pn.ProviderId
(notNetworkIds.Contains(pn.Network))
select p;
practitioners = practitioners
.Distinct()
.OrderByDescending(p => p.UpdateDate);
data = practitioners.Skip(PageSize * (pageOffset ?? 0)).Take(PageSize).ToList();
一切正常,但使用distinct
的效率非常低。较大的结果集会产生令人无法接受的性能。 DISTINCT
正在扼杀我。仅需要distinct,因为可以查询多个网络,从而导致Providers记录被复制。实际上,我需要要求DB“只返回提供者ONCE,即使他们在多个网络中”。如果我可以将DISTINCT
放在ProviderNetworks上,则查询运行得更快。
如何让EF只添加子查询DISTINCT
,而不是整个结果集?
我不想要的简化sql是:
select DISTINCT p.* from Providers
inner join Networks pn on p.ProviderId = pn.ProviderId
where NetworkName in ('abc','def')
IDEAL sql是:
select p.* from Providers
inner join (select DISTINCT ProviderId from Networks
where NetworkName in ('abc','def'))
as pn on p.ProviderId = pn.ProviderId
由于 戴夫
答案 0 :(得分:6)
我认为你不需要Distinct
,而是需要Exists
(或者在Linq中调用Any
)试试这个:
var q = (from p in context.Practitioners
where context.ProviderNetworks.Any(pn => pn.ProviderId == p.ProviderId && notNetworkIds.Contains(pn.Network))
orderby p.UpdateDate descending
select p).Skip(PageSize * (pageOffset ?? 0)).Take(PageSize).ToList();