Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Extensions / UI / WebControls / Expressions / SearchExpression.cs / 1305376 / SearchExpression.cs
#if ORYX_VNEXT namespace Microsoft.Web.Data.UI.WebControls.Expressions { #else namespace System.Web.UI.WebControls.Expressions { #endif using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Linq.Expressions; using System.Web.Resources; using System.Web.UI; using System.Web.UI.WebControls; public class SearchExpression : ParameterDataSourceExpression { public string DataFields { get { return (string)ViewState["DataFields"] ?? String.Empty; } set { ViewState["DataFields"] = value; } } public SearchType SearchType { get { object o = ViewState["SearchType"]; return o != null ? (SearchType)o : SearchType.StartsWith; } set { ViewState["SearchType"] = value; } } public StringComparison ComparisonType { get { object o = ViewState["ComparisonType"]; return o != null ? (StringComparison)o : StringComparison.OrdinalIgnoreCase; } set { ViewState["ComparisonType"] = value; } } public override IQueryable GetQueryable(IQueryable source) { if (source == null) { return null; } if ((DataFields == null) || String.IsNullOrEmpty(DataFields.Trim())) { throw new InvalidOperationException(AtlasWeb.Expressions_DataFieldRequired); } IDictionaryvalues = GetValues(); if (values.Count == 0) { throw new InvalidOperationException(AtlasWeb.SearchExpression_ParameterRequired); } string query = Convert.ToString(values.First().Value, CultureInfo.CurrentCulture); if (String.IsNullOrEmpty(query)) { return null; } string[] properties = DataFields.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // Use the or expression to or the fields together List searchExpressions = new List (); ParameterExpression parameterExpression = Expression.Parameter(source.ElementType, String.Empty); foreach (string p in properties) { Expression property = ExpressionHelper.CreatePropertyExpression(parameterExpression, p.Trim()); searchExpressions.Add(CreateCallExpression(property, query)); } return ExpressionHelper.Where(source, Expression.Lambda(ExpressionHelper.Or(searchExpressions), parameterExpression)); } private Expression CreateCallExpression(Expression property, string query) { // LINQ to SQL does not support the overloads StartsWith(string, StringComparer) or EndsWith(string, StringComparer) // and Contains has not overload that takes a StringComparer if (SearchType == SearchType.Contains || (ViewState["ComparisonType"] == null)) { return Expression.Call(property, SearchType.ToString(), Type.EmptyTypes, Expression.Constant(query, property.Type)); } return Expression.Call(property, SearchType.ToString(), Type.EmptyTypes, Expression.Constant(query, property.Type), Expression.Constant(ComparisonType)); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. #if ORYX_VNEXT namespace Microsoft.Web.Data.UI.WebControls.Expressions { #else namespace System.Web.UI.WebControls.Expressions { #endif using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Linq.Expressions; using System.Web.Resources; using System.Web.UI; using System.Web.UI.WebControls; public class SearchExpression : ParameterDataSourceExpression { public string DataFields { get { return (string)ViewState["DataFields"] ?? String.Empty; } set { ViewState["DataFields"] = value; } } public SearchType SearchType { get { object o = ViewState["SearchType"]; return o != null ? (SearchType)o : SearchType.StartsWith; } set { ViewState["SearchType"] = value; } } public StringComparison ComparisonType { get { object o = ViewState["ComparisonType"]; return o != null ? (StringComparison)o : StringComparison.OrdinalIgnoreCase; } set { ViewState["ComparisonType"] = value; } } public override IQueryable GetQueryable(IQueryable source) { if (source == null) { return null; } if ((DataFields == null) || String.IsNullOrEmpty(DataFields.Trim())) { throw new InvalidOperationException(AtlasWeb.Expressions_DataFieldRequired); } IDictionary values = GetValues(); if (values.Count == 0) { throw new InvalidOperationException(AtlasWeb.SearchExpression_ParameterRequired); } string query = Convert.ToString(values.First().Value, CultureInfo.CurrentCulture); if (String.IsNullOrEmpty(query)) { return null; } string[] properties = DataFields.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // Use the or expression to or the fields together List searchExpressions = new List (); ParameterExpression parameterExpression = Expression.Parameter(source.ElementType, String.Empty); foreach (string p in properties) { Expression property = ExpressionHelper.CreatePropertyExpression(parameterExpression, p.Trim()); searchExpressions.Add(CreateCallExpression(property, query)); } return ExpressionHelper.Where(source, Expression.Lambda(ExpressionHelper.Or(searchExpressions), parameterExpression)); } private Expression CreateCallExpression(Expression property, string query) { // LINQ to SQL does not support the overloads StartsWith(string, StringComparer) or EndsWith(string, StringComparer) // and Contains has not overload that takes a StringComparer if (SearchType == SearchType.Contains || (ViewState["ComparisonType"] == null)) { return Expression.Call(property, SearchType.ToString(), Type.EmptyTypes, Expression.Constant(query, property.Type)); } return Expression.Call(property, SearchType.ToString(), Type.EmptyTypes, Expression.Constant(query, property.Type), Expression.Constant(ComparisonType)); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ProjectionPruner.cs
- MessageRpc.cs
- HiddenField.cs
- Enum.cs
- RequestNavigateEventArgs.cs
- RepeaterCommandEventArgs.cs
- NameValueCollection.cs
- SiteMapProvider.cs
- NameValuePair.cs
- Run.cs
- QilFactory.cs
- RSAProtectedConfigurationProvider.cs
- ArraySegment.cs
- PageThemeBuildProvider.cs
- PageParser.cs
- FaultException.cs
- SQLSingle.cs
- PolygonHotSpot.cs
- DeviceContext.cs
- LayoutEngine.cs
- PersonalizationDictionary.cs
- ClaimTypeElement.cs
- EntityStoreSchemaGenerator.cs
- GridLengthConverter.cs
- TableProviderWrapper.cs
- CodeIdentifier.cs
- ActionItem.cs
- TrackingStringDictionary.cs
- OdbcConnection.cs
- XmlNodeWriter.cs
- CompiledQueryCacheKey.cs
- SystemIPInterfaceProperties.cs
- XmlAttributes.cs
- Calendar.cs
- Compilation.cs
- DataSourceComponent.cs
- DataTableTypeConverter.cs
- EventLogTraceListener.cs
- DataControlCommands.cs
- GeneralTransform2DTo3DTo2D.cs
- XmlAttributeAttribute.cs
- Point.cs
- TextSpan.cs
- SqlTypeSystemProvider.cs
- TransformValueSerializer.cs
- UserPreferenceChangedEventArgs.cs
- PointAnimation.cs
- QueryAsyncResult.cs
- Storyboard.cs
- LowerCaseStringConverter.cs
- StdValidatorsAndConverters.cs
- MobileListItem.cs
- SplashScreen.cs
- FormsAuthenticationModule.cs
- DisableDpiAwarenessAttribute.cs
- UnorderedHashRepartitionStream.cs
- DynamicResourceExtensionConverter.cs
- PreservationFileReader.cs
- PrintSystemException.cs
- SqlTypeSystemProvider.cs
- RequestCacheManager.cs
- OracleColumn.cs
- HyperLinkField.cs
- XPathParser.cs
- HtmlElementCollection.cs
- SetterBaseCollection.cs
- DataServiceContext.cs
- ConstructorNeedsTagAttribute.cs
- RsaSecurityToken.cs
- ClassImporter.cs
- FileSecurity.cs
- PenContexts.cs
- ConfigurationCollectionAttribute.cs
- OletxVolatileEnlistment.cs
- RepeaterItemCollection.cs
- Ref.cs
- PowerEase.cs
- ExpressionBuilder.cs
- ServiceModelEnhancedConfigurationElementCollection.cs
- VBCodeProvider.cs
- ScriptingJsonSerializationSection.cs
- StdValidatorsAndConverters.cs
- ProfilePropertySettings.cs
- ObjectHandle.cs
- ValueProviderWrapper.cs
- SqlConnectionFactory.cs
- HttpListenerPrefixCollection.cs
- BaseTemplateBuildProvider.cs
- LinqDataSourceContextData.cs
- Odbc32.cs
- DocumentViewerBase.cs
- MyContact.cs
- AdRotator.cs
- Stream.cs
- SerializerProvider.cs
- DrawingContext.cs
- RoutedEventValueSerializer.cs
- WebBrowserDocumentCompletedEventHandler.cs
- FrameworkContentElementAutomationPeer.cs
- EditorAttributeInfo.cs