/// <summary>
/// Returns the Where clause for specified rating filter.
/// </summary>
/// <param name="Query">The video selection query to filter.</param>
/// <param name="item">The rating filter to apply.</param>
/// <param name="ratingRatio">The rating ratio.</param>
/// <param name="context">The data context to the database.</param>
/// <returns>The Where clause to apply to the query.</returns>
public static Expression<Func<Media, bool>> GetFilterClause(IQueryable<Media> Query, SearchRatingSetting item, double ratingRatio, Entities context) {
Expression<Func<Media, bool>> Result;
if (item.Category == "Length")
Result = (v => context.CompareValues(v.Length, item.Operator, item.Value * 60));
else if (item.Category == "Height")
Result = (v => context.CompareValues(v.Height, item.Operator, item.Value));
else if (item.Category == "Preference")
Result = (v => context.CompareValues(v.Preference, item.Operator, item.Value));
else if (item.Category == "Highest")
Result = (v => context.CompareValues(v.MediaRatings.Max(r => r.DbGetValue(r.Height, r.Depth, ratingRatio)), item.Operator, item.Value));
else if (item.Category.StartsWith("!Physical") || item.Category.StartsWith("!Emotional") || item.Category.StartsWith("!Spiritual")) {
// All other polarity energies smaller than...
string ItemCategory = item.Category.Substring(1);
Result = (v =>
!(from r in v.MediaRatings
let val = r.DbGetValue(r.Height, r.Depth, ratingRatio)
where !r.RatingCategory.Name.StartsWith(ItemCategory) && r.RatingCategory.Custom == false &&
r.RatingCategory.Name != "Egoless" && r.RatingCategory.Name != "Love" && (
(item.Operator == OperatorConditionEnum.Smaller && val >= item.Value))
select 1).Any());
} else if (item.Category.StartsWith("!")) {
// All other energies smaller than...
string ItemCategory = item.Category.Substring(1);
Result = (v =>
!(from r in v.MediaRatings
let val = r.DbGetValue(r.Height, r.Depth, ratingRatio)
where r.RatingCategory.Name != ItemCategory && (
(item.Operator == OperatorConditionEnum.Smaller && val >= item.Value))
select 1).Any());
} else if (item.Category.StartsWith("Intensity"))
// The average of the 5 highest values.
Result = (v =>
(from t in v.MediaRatings
let val = (from r in v.MediaRatings
let val = r.DbGetValue(r.Height, r.Depth, ratingRatio)
orderby val descending
select val).Take(5).Average()
where context.CompareValues(val, item.Operator, item.Value)
select 1).Any());
else if (item.Operator != OperatorConditionEnum.Smaller) {
// Standard rating filters.
Result = (v =>
(from t in
(from r in v.MediaRatings
where r.RatingCategory.Name.StartsWith(item.Category)
orderby r.DbGetValue(r.Height, r.Depth, ratingRatio) descending
select r).Take(1)
let val = t.DbGetValue(t.Height, t.Depth, ratingRatio)
where context.CompareValues(val, item.Operator, item.Value)
select 1).Any());
} else {
// Standard rating filter with < operator.
Result = (v =>
(from t in
(from r in v.MediaRatings
where r.RatingCategory.Name.StartsWith(item.Category)
orderby r.DbGetValue(r.Height, r.Depth, ratingRatio) descending
select r).Take(1)
let val = t.DbGetValue(t.Height, t.Depth, ratingRatio)
let val2 = t.DbGetValue(t.Height, t.Depth, 0) // '<' operator applies to both specified ratio and ratio 0.
where val >= item.Value && val2 >= item.Value
select 1).Any() == false);
}
// Apply 'or' filter.
if (item.Or != null)
Result = Result.OrElse(GetFilterClause(Query, item.Or, ratingRatio, context));
return Result;
}