Query filter where statement

Topics: General, Troubleshooting, Writing modules
Aug 26, 2011 at 7:39 AM

Is it possible to use the _orchardServices.ContentManager.Query function including a where statement where the where statement is generated in to the sql query? So i don't retrieve a big array in memory and filter it afterwards?

Coordinator
Aug 26, 2011 at 7:44 AM

Sure. Do a join.

Aug 26, 2011 at 10:41 AM

How do you mean a join? Because with a join a can't filter right? I have the following query but it the where statement doesn't work:

var result = _orchardServices.ContentManager.Query(VersionOptions.Published, "CommunityMyMessage")
                .Join<CommunityMyMessagePartRecord>().Where(s =>
                        s.UserPartRecordSender != null &&
                        s.UserPartRecordSender.Id == _orchardServices.WorkContext.CurrentUser.Id);

Aug 26, 2011 at 1:57 PM

If what you want to query on is the CommunityMyMessage part/record, you can use Where without doing a Join if you use the generic overload of the Query method. Note, this overload is an extension method in the Orchard.ContentManagement namespace, so you need to have that imported. You can do something like this:

var result = _orchardServices.ContentManager.Query<CommunityMyMessagePart,CommunityMyMessagePartRecord>()
                .Where(r =>
                          //r is typed as CommunityMyMessagePartRecord here, so you can do whatever filter you want on that
                      ).List();
Doing this, the filter will happen in the sql query and result will be an IEnumerable<CommunityMyMessagePart>.

Coordinator
Aug 26, 2011 at 6:10 PM

Also, look for examples of usage of Join and Where in the source code. For example in ItemController.