/// <summary> /// Builds an <see cref="IQueryable"/> from the <paramref name="queryPredicate"/> and an optional <paramref name="leftHandQueryable"/>. /// This recursively calls itself and may call <see cref="GeneratePredicateIsNotLogicallyDeleted"/>. /// </summary> protected virtual IQueryable <TData> GeneratePredicate(IQueryPredicate queryPredicate, IQueryable <TData> leftHandQueryable = null) { var andQueryPredicate = queryPredicate as IAndQueryPredicate; if (andQueryPredicate != null) { IQueryable <TData> innerLeftHandQueryable = GeneratePredicate(andQueryPredicate.LeftQueryPredicate); return(GeneratePredicate(andQueryPredicate.RightQueryPredicate, innerLeftHandQueryable)); } var orQueryPredicate = queryPredicate as IOrQueryPredicate; if (orQueryPredicate != null) { IQueryable <TData> innerLeftHandQueryable = GeneratePredicate(orQueryPredicate.LeftQueryPredicate); return(GeneratePredicate(orQueryPredicate.RightQueryPredicate, innerLeftHandQueryable)); } var realQueryPredicate = queryPredicate as QueryPredicate; if (realQueryPredicate != null) { IQueryable <TData> result = GeneratePredicateIsNotLogicallyDeleted(realQueryPredicate, leftHandQueryable); return(result ?? GeneratePredicate(realQueryPredicate, leftHandQueryable)); } throw new InvalidOperationException(string.Format("The query predicate '{0}' is unable to be processed.", queryPredicate == null ? typeof(void) : queryPredicate.GetType())); }
/// <summary> /// Builds predicate for selected QueryOptionItem in ToolStripDropDownButton</summary> /// <param name="predicate">Search predicates</param> public override void BuildPredicate(IQueryPredicate predicate) { if (m_selectedItem != null) { m_selectedItem.BuildPredicate(predicate); } }
/// <summary> /// Compiles an enumeration of DomNode properties (as objects) that matched the conditions of the search predicates</summary> /// <param name="predicate">Specifies the test conditions for a query</param> /// <returns>The enumeration of DomNode properties (e.g., DomNodeQueryMatch) satisfying the query</returns> public IEnumerable <object> Query(IQueryPredicate predicate) { m_results.Clear(); // Iterate over all dom nodes under this adapter foreach (DomNode domNode in DomNode.Subtree) { // The results of one DomNode query associate each predicate with matching dom node properties Dictionary <IQueryPredicate, IList <IQueryMatch> > predicateMatchResults = new Dictionary <IQueryPredicate, IList <IQueryMatch> >(); // For each queryable item (ie a DomNode) there may be 0 to many "query matches" // (ie a DomNode property). On success, predicate.Test() will supply one // IQueryMatch per DomNode property that matched. IList <IQueryMatch> matchingPropertiesList; if (predicate.Test(domNode, out matchingPropertiesList)) { if (matchingPropertiesList != null) { predicateMatchResults[predicate] = matchingPropertiesList; } // For this queryable, a match is the DomNode that passed the predicate test, // paired with all its properties that allowed it to satisfy the test m_results.Add(new DomNodeQueryMatch(domNode, predicateMatchResults)); } } // Announce that the search results have changed ResultsChanged.Raise(this, EventArgs.Empty); return(m_results); }
/// <summary> /// Compiles an enumeration of DomNode properties (as objects) that matched the conditions of the search predicates</summary> /// <param name="predicate">Specifies the test conditions for a query</param> /// <returns>The enumeration of DomNode properties (e.g., DomNodeQueryMatch) satisfying the query</returns> public IEnumerable<object> Query(IQueryPredicate predicate) { m_results.Clear(); // Iterate over all dom nodes under this adapter foreach (DomNode domNode in DomNode.Subtree) { // The results of one DomNode query associate each predicate with matching dom node properties Dictionary<IQueryPredicate, IList<IQueryMatch>> predicateMatchResults = new Dictionary<IQueryPredicate, IList<IQueryMatch>>(); // For each queryable item (ie a DomNode) there may be 0 to many "query matches" // (ie a DomNode property). On success, predicate.Test() will supply one // IQueryMatch per DomNode property that matched. IList<IQueryMatch> matchingPropertiesList; if (predicate.Test(domNode, out matchingPropertiesList)) { if (matchingPropertiesList != null) predicateMatchResults[predicate] = matchingPropertiesList; // For this queryable, a match is the DomNode that passed the predicate test, // paired with all its properties that allowed it to satisfy the test m_results.Add(new DomNodeQueryMatch(domNode, predicateMatchResults)); } } // Announce that the search results have changed ResultsChanged.Raise(this, EventArgs.Empty); return m_results; }
private static Expression <Func <T, bool> > GetWherePredicateExpression <T>([NotNull] IQueryPredicate <QueryPredicateValue> predicate) where T : class { if (predicate.Values == null) { throw new QueryPredicateException("Predicate cannot exist without values to compare"); } var parameter = Expression.Parameter(typeof(T), "e"); Expression predicateExpression = null; foreach (var queryPredicateValue in predicate.Values) { var predicateValueExpression = GetWherePredicateValueExpression(typeof(T), parameter, predicate, queryPredicateValue); predicateExpression = predicateExpression == null ? predicateValueExpression : queryPredicateValue.CompareWith == QueryPredicateConnective.And ? Expression.And(predicateExpression, predicateValueExpression) : Expression.OrElse(predicateExpression, predicateValueExpression); } if (predicateExpression == null) { throw new QueryPredicateException($"A valid Expression could not be constructed for predicate with column code '{predicate.ColumnCode}'."); } return(Expression.Lambda <Func <T, bool> >(predicateExpression, parameter)); }
/// <summary> /// Builds predicate from all children recursively</summary> /// <param name="predicate">Search predicates</param> public virtual void BuildPredicate(IQueryPredicate predicate) { foreach (QueryNode child in Children) { child.BuildPredicate(predicate); } }
public void DoCommand(object commandTag) { if (!(commandTag is Command)) { return; } switch ((Command)commandTag) { case Command.SearchForPlacements: { var target = _resLister.ContextMenuTarget; if (target == null) { break; } IQueryPredicate predicate = null; // note -- we could use the IResourceService to do the type resolution here by // calling IResourceService.Load... However, if the load operation is // expensive, we might not always want to do it. var ext = System.IO.Path.GetExtension(target.LocalPath); if (IsModelExtension(ext)) { predicate = XLEPlacementObject.CreateSearchPredicate(target); } else if (IsModelBookmarkExtension(ext)) { // todo -- we could load the bookmark via the resource service, as so -- // var res = IResourceService.Load(target); // if (res == null) break; var bookmark = XLEPlacementObject.LoadBookmark(target); if (bookmark == null) { break; } predicate = XLEPlacementObject.CreateSearchPredicate(bookmark, target); } if (predicate != null) { var queryContext = _contextRegistry.GetActiveContext <IQueryableContext>(); if (queryContext != null) { // note -- in our query context, the results will be displaced to the user automatically // we could "show" the search results ui here, however. queryContext.Query(predicate); if (_searchService != null) { _searchService.Show(); } } } } break; } }
/// <summary> /// Builds an <see cref="IOrQueryPredicate"/> between <see cref="QueryPredicate"/> /// and the provided <paramref name="queryPredicate"/> /// </summary> protected virtual IQueryPredicate Or(IQueryPredicate queryPredicate) { return(new OrQueryPredicate { LeftQueryPredicate = QueryPredicate, RightQueryPredicate = queryPredicate }); }
/// <summary> /// Adds a search for DomNode property values with a matching number</summary> /// <param name="predicate">Predicate to which the property search expression will be added</param> /// <exception cref="ArgumentException">DomNode-specific query tree has been passed an unhandled type of predicate info</exception> public override void BuildPredicate(IQueryPredicate predicate) { // No predicate added if the text input can't be parsed as a number Double num; if (Double.TryParse(TextInput1, out num) == false) { return; } DomNodePropertyPredicate domNodePredicate = (DomNodePropertyPredicate)predicate; if (domNodePredicate == null) { throw new ArgumentException("DomNode-specific query tree has been passed an unhandled type of predicate info"); } // Add appropriate search expression to predicate switch (SelectedItem.Tag) { case (UInt64)NumericalQuery.Equals: domNodePredicate.AddNumberValueEqualsExpression(num, m_isReplacePattern); break; case (UInt64)NumericalQuery.Lesser: domNodePredicate.AddNumberValueLesserExpression(num, m_isReplacePattern); break; case (UInt64)NumericalQuery.LesserEqual: domNodePredicate.AddNumberValueLesserEqualExpression(num, m_isReplacePattern); break; case (UInt64)NumericalQuery.GreaterEqual: domNodePredicate.AddNumberValueGreaterEqualExpression(num, m_isReplacePattern); break; case (UInt64)NumericalQuery.Greater: domNodePredicate.AddNumberValueGreaterExpression(num, m_isReplacePattern); break; case (UInt64)NumericalQuery.Between: { Double num2; if (Double.TryParse(TextInput2, out num2)) { domNodePredicate.AddNumberValueBetweenExpression(num, num2, m_isReplacePattern); } break; } default: // throw exception...? break; } }
/// <summary> /// Adds a search for DomNode property values with a matching string</summary> /// <param name="predicate">Predicate to which the property search expression will be added</param> /// <exception cref="ArgumentException">DomNode-specific query tree has been passed an unhandled type of predicate info</exception> public override void BuildPredicate(IQueryPredicate predicate) { DomNodePropertyPredicate domNodePropertyPredicate = (DomNodePropertyPredicate)predicate; if (domNodePropertyPredicate == null) { throw new ArgumentException("DomNode-specific query tree has been passed an unhandled type of predicate info"); } BuildStringPredicate(domNodePropertyPredicate, DomNodeQuery.PropertySearchTarget.Value); }
/// <summary> /// Adds a search for DomNode properties with name "Name", and with a matching string value</summary> /// <param name="predicate">Predicate to which the property search expression will be added</param> /// <exception cref="ArgumentException">DomNode-specific query tree has been passed an unhandled type of predicate info</exception> public override void BuildPredicate(IQueryPredicate predicate) { DomNodePropertyPredicate domNodePredicate = (DomNodePropertyPredicate)predicate; if (domNodePredicate == null) { throw new ArgumentException("DomNode-specific query tree has been passed an unhandled type of predicate info"); } domNodePredicate.AddPropertyNameExpression("Name"); base.BuildPredicate(domNodePredicate); }
/// <summary> /// Builds an <see cref="IAndQueryPredicate"/> between <see cref="QueryPredicate"/> /// and the provided <paramref name="queryPredicate"/> /// </summary> protected virtual IQueryPredicate And(IQueryPredicate queryPredicate) { if (QueryPredicate == null) { return(queryPredicate); } return(new AndQueryPredicate { LeftQueryPredicate = QueryPredicate, RightQueryPredicate = queryPredicate }); }
private static Expression GetWherePredicateValueEnumerablePropertyExpression( [NotNull] PropertyInfo property, [NotNull] ParameterExpression parameter, [NotNull] IQueryPredicate <QueryPredicateValue> predicate, [NotNull] IQueryPredicateValue predicateValue) { var c = Expression.PropertyOrField(parameter, predicate.ColumnCode.Split('.')[0]); // list property on parent var a = property.PropertyType.GetGenericArguments()[0]; // list property on parent: item type var p = Expression.Parameter(a, "c"); // comparison on list item type: left var v = GetWherePredicateValueComparisonExpression(a, p, predicate, predicateValue); // comparison on list item type: right var l = Expression.Lambda(v, p); // comparison on list item type return(Expression.Call(typeof(Enumerable), "Any", new[] { a }, c, l)); }
private static Expression GetWherePredicateValueComparisonExpression( [NotNull] Type type, [NotNull] Expression parameter, [NotNull] IQueryPredicate <QueryPredicateValue> predicate, [NotNull] IQueryPredicateValue predicateValue) { var code = predicate.ColumnCode.Split('.').Last().Capitalize(); var property = type.GetProperty(code); if (property == null) { throw new QueryPredicateException($"Predicate compares unknown property {code}."); } var value = GetTypedValue(property, predicateValue.Value); switch (predicateValue.CompareUsing) { case QueryPredicateComparison.Equal: return(Expression.Equal(Expression.Property(parameter, property), value)); case QueryPredicateComparison.NotEqual: return(Expression.NotEqual(Expression.Property(parameter, property), value)); case QueryPredicateComparison.LessThan: return(Expression.LessThan(Expression.Property(parameter, property), value)); case QueryPredicateComparison.LessThanOrEqual: return(Expression.LessThanOrEqual(Expression.Property(parameter, property), value)); case QueryPredicateComparison.GreaterThan: return(Expression.GreaterThan(Expression.Property(parameter, property), value)); case QueryPredicateComparison.GreaterThanOrEqual: return(Expression.GreaterThanOrEqual(Expression.Property(parameter, property), value)); case QueryPredicateComparison.StartsWith: case QueryPredicateComparison.EndsWith: case QueryPredicateComparison.Contains: return(property.PropertyType == typeof(string) ? StringPropertyFilter(parameter, property, predicateValue.Value, predicateValue.CompareUsing) : throw new QueryPredicateValueException($"Property '{code}' on type {type.Name} should be of type string to use {predicateValue.CompareUsing} as comparison type.")); default: throw new QueryPredicateValueException($"Predicate value uses unsupported compare type ({predicateValue.CompareUsing})."); } }
private static Expression GetWherePredicateValueComplexPropertyExpression( [NotNull] Type type, [NotNull] PropertyInfo property, [NotNull] ParameterExpression parameter, [NotNull] IQueryPredicate <QueryPredicateValue> predicate, [NotNull] IQueryPredicateValue predicateValue) { Expression body = parameter; foreach (var memberCode in predicate.ColumnCode.Split('.')) { var memberProperty = type.GetProperty(memberCode); if (memberProperty != null && (!IsComparableProperty(memberProperty) && !memberProperty.PropertyType.Implements <IEnumerable>())) { property = memberProperty; body = Expression.PropertyOrField(body, memberCode); } } return(GetWherePredicateValueComparisonExpression(property.PropertyType, body, predicate, predicateValue)); }
/// <summary> /// Gets the expression that represents the supplied <paramref name="predicateValue"/>'s comparison as part of a where clause's <paramref name="predicate"/>. /// </summary> /// <param name="type">The type.</param> /// <param name="parameter">The "left-side" part of a .</param> /// <param name="predicate">A where clause for a (sub)property query.</param> /// <param name="predicateValue">The value the <paramref name="predicate"/>'s property should match with using the <seealso cref="QueryPredicateValue.CompareUsing"/>.</param> /// <returns>A <paramref name="predicate"/>'s single value comparison expression.</returns> /// <exception cref="QueryPredicateException">Predicate compares unknown property {code}.</exception> private static Expression GetWherePredicateValueExpression( [NotNull] Type type, [NotNull] ParameterExpression parameter, [NotNull] IQueryPredicate <QueryPredicateValue> predicate, [NotNull] IQueryPredicateValue predicateValue) { if (predicate.ColumnCode.Contains('.')) { var code = predicate.ColumnCode.Split('.')[0]; var property = type.GetProperty(code); if (property == null) { throw new QueryPredicateException($"Predicate compares unknown property {code}."); } return(property.PropertyType.Implements <IEnumerable>() ? GetWherePredicateValueEnumerablePropertyExpression(property, parameter, predicate, predicateValue) : GetWherePredicateValueComplexPropertyExpression(type, property, parameter, predicate, predicateValue)); } return(GetWherePredicateValueComparisonExpression(type, parameter, predicate, predicateValue)); }
/// <summary> /// 查询条件 /// </summary> /// <param name="predicate">查询谓语</param> /// <returns></returns> public IExactValueSearchCondition <T> Where(IQueryPredicate <T> predicate) { this.QueryPredicate = predicate; return(this); }
public WhereClause(IQueryPredicate predicate) { Predicate = predicate; }
/// <summary> /// Adds a search for DomNode properties with name "Name", and with a matching string value</summary> /// <param name="predicate">Predicate to which the property search expression is added</param> /// <exception cref="ArgumentException">DomNode-specific query tree has been passed an unhandled type of predicate info</exception> public override void BuildPredicate(IQueryPredicate predicate) { DomNodePropertyPredicate domNodePredicate = (DomNodePropertyPredicate)predicate; if (domNodePredicate == null) throw new ArgumentException("DomNode-specific query tree has been passed an unhandled type of predicate info"); domNodePredicate.AddPropertyNameExpression("Name"); base.BuildPredicate(domNodePredicate); }
/// <summary> /// Adds a search for DomNode property values with a matching number</summary> /// <param name="predicate">Predicate to which the property search expression is added</param> /// <exception cref="ArgumentException">DomNode-specific query tree has been passed an unhandled type of predicate info</exception> public override void BuildPredicate(IQueryPredicate predicate) { // No predicate added if the text input can't be parsed as a number Double num; if (Double.TryParse(TextInput1, out num) == false) return; DomNodePropertyPredicate domNodePredicate = (DomNodePropertyPredicate)predicate; if (domNodePredicate == null) throw new ArgumentException("DomNode-specific query tree has been passed an unhandled type of predicate info"); // Add appropriate search expression to predicate switch (SelectedItem.Tag) { case (UInt64)NumericalQuery.Equals: domNodePredicate.AddNumberValueEqualsExpression(num, m_isReplacePattern); break; case (UInt64)NumericalQuery.Lesser: domNodePredicate.AddNumberValueLesserExpression(num, m_isReplacePattern); break; case (UInt64)NumericalQuery.LesserEqual: domNodePredicate.AddNumberValueLesserEqualExpression(num, m_isReplacePattern); break; case (UInt64)NumericalQuery.GreaterEqual: domNodePredicate.AddNumberValueGreaterEqualExpression(num, m_isReplacePattern); break; case (UInt64)NumericalQuery.Greater: domNodePredicate.AddNumberValueGreaterExpression(num, m_isReplacePattern); break; case (UInt64)NumericalQuery.Between: { Double num2; if (Double.TryParse(TextInput2, out num2)) domNodePredicate.AddNumberValueBetweenExpression(num, num2, m_isReplacePattern); break; } default: // throw exception...? break; } }
/// <summary> /// Adds a search for DomNode property values with a matching string</summary> /// <param name="predicate">Predicate to which the property search expression is added</param> /// <exception cref="ArgumentException">DomNode-specific query tree has been passed an unhandled type of predicate info</exception> public override void BuildPredicate(IQueryPredicate predicate) { DomNodePropertyPredicate domNodePropertyPredicate = (DomNodePropertyPredicate)predicate; if (domNodePropertyPredicate == null) throw new ArgumentException("DomNode-specific query tree has been passed an unhandled type of predicate info"); BuildStringPredicate(domNodePropertyPredicate, DomNodeQuery.PropertySearchTarget.Value); }
public HavingClause(IQueryPredicate predicate) { Predicate = predicate; }
/// <summary> /// Builds search predicate for the text box entries</summary> /// <param name="predicate">Test conditions and value replacement info for query</param> public override void BuildPredicate(IQueryPredicate predicate) { // Override to create some sort of search predicate for the text box entry }
/// <summary> /// 设置过滤条件 /// </summary> /// <param name="predicate">过滤条件谓语</param> /// <returns></returns> public IFullTextSearchCondition <T> Filter(IQueryPredicate <T> predicate) { this.FilterPredicate = predicate; return(this); }