我有以下关系:
我想从Ads中选择与FeaturesOfAds相关的所有行,所以我这样做:
query = from ad in query
join featureOfAd in db.FeaturesOfAds on ad.AdId equals featureOfAd.AdId
where options.FeatureIds.Contains(featureOfAd.FeatureId)
select ad;
options.FeatureIds
是一个int数组。它的工作原理但问题是结果返回了很多重复的行,我需要进行Distinct
调用,我担心它可能会在高负载和数千行的情况下表现不佳。所以我想知道是否有更好更有效的方法来做到这一点。
答案 0 :(得分:0)
您应该按AdID对结果进行分组,以获得不同的值。 可能没有更好的解决方案然后加入团队。你可以尝试存在,但我怀疑它会慢得多。
var result = (from a in Ads
join foa in FeatureOfAds on a.AdID equals foa.AdID
where FeatureIDs.Contains(foa.FeatureID)
group a by a.AdID into gr
select gr.Key).ToList();
var result = (from a in Ads
where a.FeatureOfAds.Any(c=>FeatureIDs.Contains(c.FeatureID))
select a.AdID).ToList();