下面的代码片段描述了我想使用查询做什么,但它会因上述错误而爆炸.
建筑物和AttributeValues之间存在一对多的关系,我的目标是找到所有具有“大”的AttributeValue和“Blue”的AttributeValue的建筑物.
var attributeValueAlias1 = new AttributeValue(); var attributeValueAlias2 = new AttributeValue(); var result = repository .CreateCriteriaFor<Buildings>() .Join(o=>o.AttributeValues,()=> attributeValueAlias1) .Join(o=>o.AttributeValues,()=> attributeValueAlias2) .Add(() => attributeValueAlias1.Value == "Large") .Add(() => attributeValueAlias2.Value == "Blue") .List();
我可以这样做,所以在添加别名之前使用criteria.GetCriteriaByAlias(别名)添加了成员资格测试,因此不会发生Duplicate别名错误,但是只有一个连接,它会导致sql where子句永远不会为true
SELECT * FROM Buildings this_ inner join AttributeValues attributev1_ on this_.Id = attributev1_.BuildingId WHERE attributev1_.Value = 'Large' and attributev1_.Value = 'Blue'
解决方法
您不应该使用连接来执行此操作,因为您可能从异常中猜到了.您应该使用两个子查询来测试集合中的值“Blue”和“Large”.我在ICriteria上并不擅长,但生成的sql应该是这样的,我认为这在ICriteria中是可行的(使用Subqueries.PropertyIn).
select * from Buildings b inner join AttributeValues av on b.Id = av.BuildingId where av.Id in (select Id from AttributeValues av2 where av2.BuildingId=b.BuildingId and av2.Value="Large") and av.Id in (select Id from AttributeValues av3 where av3.BuildingId=b.BuildingId and av3.Value="Blue")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。