通过字段在Orchard CMS中查询ContentPart

3

我在Orchard 1.4中工作,有一个名为ProductPart的内容部件,其中包含一个IsFeatured布尔字段。在Orchard 1.4中,使用Projection模块轻松查询ProductPart

我想编写一个ProductService,并希望查询ProductPart,其中IsFeatured字段为true,如下所示:

contentManager.Query<ProductPart,ProductRecord>().Where(x=>x.IsFeatured).ToList()

如何获得这个?

2个回答

3
你不能这样做。字段的存储方式使得它们无法以这种方式进行查询。你可以注入IProjectionManager并在服务中使用投影器查询。或者创建一个FeaturedProduct部分,然后使用ContentManager进行查询。

"IProjectionManager" 的方法对我今天很有帮助。感谢指引,但似乎网络上只有几个关于该类的结果。 - rtpHarry

1
使用泛型的查询方法(确保使用Orchard.ContentManagement)
var products = contentManager.Query<ProductPart, ProductPartRecord>().Where(x => x.IsFeatured).ToList()

IsFeatured是一个布尔字段:<br/><pre><code>ContentDefinitionManager.AlterPartDefinition(typeof(ProductPart).Name, p => p.WithField("IsFeatured", f => f.OfType(typeof(BooleanField).Name))</code></pre> <br/> <br/> => IsFeatured不是ProductRecord的属性=>我们不能通过这种方式查询。<br/> 有其他建议吗? - Ha Doan
这真的非常有用,谢谢!Bertrand Le Roy在这里告诉我http://stackoverflow.com/questions/16704132/retrieving-content-and-all-associated-properties-in-orchard-cms要“利用搜索模块”,但我找不到任何关于如何在代码中实现的信息。有什么想法吗? - Ben Power

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接