我有以下sql命令,我无法将其转换为linq
select Distinct(fp.Parks_Id)
from ParkFeaturePark fp
Inner Join Parkfeatures feat on fp.ParkFeatures_Id = feat.Id
Inner Join Parks p On fp.Parks_Id = p.Id
where p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =1 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =2 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =31 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =42 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =106 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =118 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =4 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =6 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =10 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =18 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =22 )
And p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =46 )
这里的扭曲是......我必须使用用户选择的组合..样本
p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =1 )
AND p.Id In (Select Parks_Id from ParkFeaturePark where ParkFeatures_Id =4 )
或用户选择任何其他组合..
感谢您的回复
答案 0 :(得分:2)
这不是答案,而是好的风格编码 -
SELECT DISTINCT(fp.Parks_Id)
FROM dbo.ParkFeaturePark fp
JOIN dbo.Parkfeatures feat ON fp.ParkFeatures_Id = feat.Id
JOIN dbo.Parks p ON fp.Parks_Id = p.Id
WHERE p.Id IN (
SELECT Parks_Id
FROM dbo.ParkFeaturePark
WHERE ParkFeatures_Id IN (1, 2, 31, 42, 106, 118, 4, 6, 10, 18, 22, 46)
)
答案 1 :(得分:2)
假设您有导航属性Park.ParkFeatureParks
和ParkFeaturePark.Parkfeatures
。 (否则将能够创建它们)。然后你可以这样做:
int[] featureIds = new { 1, 2, 31, 42, 106, 118, .. };
var query = from p in context.Parks
where p.ParkFeatureParks
.SelectMany(pfp => pfp.Parkfeatures)
.All(feature => featureIds
.Contains(id => feature.ParkFeatures_Id))
select p.Parks_Id;