Custom sort Criterion query for a custom part with not ContentPartRecord

Topics: Customizing Orchard, Troubleshooting, Writing modules
Jun 10, 2013 at 5:30 PM
Hi,

I am trying to create a custom sort criterion for projection module to sort any content based on Ratings or number of favorites. I have attached StarsPart from Orchard.Module.Contrib.Stars Module and FavoritePart from Orchard.Module.NGM.Favorite module to my content. But These modules are based on contrib.Voting module and these parts do not have corresponding ContentPartRecord.

my problem is how do I write the HQL query to sort the content, if i write query as following then i get exception that "Unable to cast object of type 'Orchard.ContentManagement.DefaultAliasFactory' to type 'StarsPart'."
 if (context.State.SortBy != null)
            {
                var _SortBy = _tokenizer.Replace(context.State.SortBy, null, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode });
                if (!string.IsNullOrWhiteSpace(_SortBy))
                {
                    switch ((string)_SortBy)
                    {
                        case "HighestRated":
                            context.Query.OrderBy(
                                alias => alias.ContentItem().As<StarsPart>(), x => x.Asc("UserRating"));
                            break;
                        case "MostFavorited":
                            context.Query.OrderBy(
                                alias => alias.ContentItem().As<FavoritePart>(), x => x.Asc("NumberOfFavorites"));
                            break;
                        default:
                             context.Query.OrderBy(
                                alias => alias.ContentItem().As<UserViewPart>(), x => x.Asc("TotalViews"));
                            break;
                    }
                }
            }
Please suggest the way to write this query.
thanks a lot.
Jun 11, 2013 at 3:42 AM
I could figure out the answer myself. Thanks All. in case if it helps others.then i wrote following query for Highest Rated content:.
if (context.State.SortBy != null)
            {
                var _SortBy = _tokenizer.Replace(context.State.SortBy, null, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode });
                if (!string.IsNullOrWhiteSpace(_SortBy))
                {
                    switch ((string)_SortBy)
                    {
                        case "HighestRated":
                                var query = (IHqlQuery)context.Query;
                                query.Join(alias => alias.ContentItem());
                                var defaultHqlQuery = query as DefaultHqlQuery;
                                var fiJoins = typeof(DefaultHqlQuery).GetField("_joins", BindingFlags.Instance | BindingFlags.NonPublic);
                                var joins = fiJoins.GetValue(defaultHqlQuery) as List<System.Tuple<IAlias, Join>>;
                                joins.Add(new System.Tuple<IAlias, Join>(new Alias("Contrib.Voting.Models"), new Join("ResultRecord", "result", ",")));
           
                                System.Collections.IDictionary dictionary = new Dictionary<string,string>();
                                dictionary.Add("Dimension","Rating");
                                dictionary.Add("FunctionName","average");

                                context.Query =  query.Where(
                                alias => alias.Named("result"),
                                predicate => predicate.EqProperty("ContentItemRecord", "ci"));
                                context.Query = context.Query.Where(
                                alias => alias.Named("result"), predicate => predicate.AllEq(dictionary));
                                context.Query = context.Query.OrderBy(alias => alias.Named("result"), x => x.Desc("Value"));
                            break;
                        case "MostFavorited":
                            context.Query.OrderBy(
                                alias => alias.ContentItem().As<FavoritePart>(), x => x.Asc("NumberOfFavorites"));
                            break;
                        default:
                             context.Query.OrderBy(
                                alias => alias.ContentItem().As<UserViewPart>(), x => x.Asc("TotalViews"));
                            break;
                    }
                }
            }
Jun 13, 2013 at 2:09 AM
While above query works but it creates inner join. I am not able to create left outer join here. This query should only sort the data based on Favorites/Rating, even if there are no votes/Ratings, it modifies the filter also. It doesn't included contents which have no favorites/ratings.

Can someone help me to write left outer join query here.
Aug 18, 2014 at 5:20 PM
I would love to see Prraggie's latest question addressed for I'm running into the same issue. I would like to bubble my list of items with ratings to the top while keeping items without ratings as well in the list. Doing this in SQL with a left join is pretty simple but using HQL via Orchard? Not sure.... so giving this discussion a bump.
Coordinator
Aug 18, 2014 at 6:32 PM
Can you do two queries groups ? This is directly in the UI, which executes two separate queries and joins them. Same thing by code.