當前位置: 首頁>>代碼示例>>C#>>正文


C# Expression.Compile方法代碼示例

本文整理匯總了C#中System.Linq.Expressions.Expression.Compile方法的典型用法代碼示例。如果您正苦於以下問題:C# Expression.Compile方法的具體用法?C# Expression.Compile怎麽用?C# Expression.Compile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Linq.Expressions.Expression的用法示例。


在下文中一共展示了Expression.Compile方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: QueryCustomers

		/// <summary>
		/// This demonstrates how to plug a query expression into an EntitySet. It prints all customers and the
		/// value of their purchases, filtering the purchases according to a specified predicate.
		/// </summary>
		static void QueryCustomers (Expression<Func<Purchase, bool>> purchasePredicate)
		{
			var data = new DemoData ();

			// We do two special things to make this query work:
			//   (1) Call AsExpandable() on the Customer table
			//   (2) Call Compile() on the supplied predicate when it's used within an EntitySet.
			// AsExpandable() returns a wrapper that strips away the call to Compile when the query is run.

			var query =
				from c in data.Customers.AsExpandable ()
				where c.Purchases.Any (purchasePredicate.Compile ())
				select new
				{
					c.Name,
					FilteredPurchases =
						from p in c.Purchases.Where (purchasePredicate.Compile ())
						select p.Price
				};

			foreach (var customerResult in query)
			{
				Console.WriteLine (customerResult.Name);
				foreach (decimal price in customerResult.FilteredPurchases)
					Console.WriteLine ("   $" + price);
			}
		}
開發者ID:osjimenez,項目名稱:LINQKit,代碼行數:31,代碼來源:Program.cs

示例2: GetTools

 public List<Tool> GetTools(Expression<Func<Tool, bool>> predicate)
 {
     if(predicate != null)
     {
       var preComp =  predicate.Compile();
       predicate = o => preComp(o) && o.Active == true;
       return ausv1Context.Tools.Where(predicate.Compile()).ToList();
     }            
     return ausv1Context.Tools.Where(o => o.Active == true).ToList();
 }
開發者ID:MTeuser,項目名稱:WebTools,代碼行數:10,代碼來源:DataAccess.cs

示例3: Find

 public IEnumerable<User> Find(Expression<Func<User, bool>> where)
 {
     using (var db = new UserContext())
     {
         return db.Users.Where(where.Compile()).ToArray();
     }
 }
開發者ID:kv1z,項目名稱:Panzar,代碼行數:7,代碼來源:UserRepository.cs

示例4: TestFilter

 private async Task TestFilter(Expression<Func<Order, bool>> predicate)
 {
     var table = Client.GetTable<Order>();
     var items = await table.Where(predicate).OrderBy(o => o.Id).ToListAsync();
     var expected = Orders.Where(predicate.Compile()).OrderBy(o => o.Id).ToArray();
     ComparisonHelper.CompareAssert(expected, items);
 }
開發者ID:aeee98,項目名稱:blogsamples,代碼行數:7,代碼來源:FilterTests.cs

示例5: AssertJiraHttpLink

 private void AssertJiraHttpLink(Expression<Func<IHttpInterfaceConfiguration, string>> configPropertyAccessor)
 {
     IHttpInterfaceConfiguration httpConfig = JiraConfig.Instance.HttpInterfaceConfiguration;
     Assert.AreEqual(
             ExpectedUrlFrom(configPropertyAccessor),
             configPropertyAccessor.Compile().Invoke(httpConfig));
 }
開發者ID:adrianoc,項目名稱:binboo,代碼行數:7,代碼來源:JiraConfigTestCase.Helper.cs

示例6: CheckExpression

 public static void CheckExpression(Expression<Func<Boolean>> expression)
 {
     if (!expression.Compile()())
     {
         throw new MalformedException(string.Concat("Bad file format, expecting ", expression.ToString().Replace("() => ", string.Empty)));
     }
 }
開發者ID:redrocktx,項目名稱:shellify,代碼行數:7,代碼來源:FormatChecker.cs

示例7: EntryTokenizeFormats

        private void EntryTokenizeFormats(string token, Func<string[],string[]> tokenizer, Expression<Func<string[]>> expression)
        {
            var key = ExpressionHelper.GetExpressionText(expression);
            var formats = tokenizer(expression.Compile().Invoke().ToArray());

            TokenizeFormats[token + ":" + key] = formats;
        }
開發者ID:takepara,項目名稱:MvcPhotos,代碼行數:7,代碼來源:UaRazorViewEngine.cs

示例8: GradientDescent

        public static Vector GradientDescent(
            Expression<Func<Vector, double>> function, 
            Expression<Func<Vector, Vector>> gradient, 
            Vector x0, 
            int iter, 
            double stepSize, 
            out Vector trajectory)
        {
            trajectory = Vector.Zeros(iter);
            var x = x0;
            var obj = double.MaxValue;
            var f = function.Compile();
            var g = gradient.Compile();
            for (int i = 0; i < iter; i++)
            {
                // calculate gradient
                var grad = g(x);
                // get step size (avoid overstepping)
                var eta = stepSize / System.Math.Sqrt(iter + 1);
                // take step
                x = x - eta * grad;
                // calculate objective
                trajectory[i] = f(x);
                // sanity check (don't want to take too many steps)
                if (System.Math.Abs(obj - trajectory[i]) < MIN_DIFF)
                    break;
                // start again
                obj = trajectory[i];
            }

            return x;
        }
開發者ID:al-main,項目名稱:CloudyBank,代碼行數:32,代碼來源:OptimizeBy.cs

示例9: AddRequestProperty

        public static void AddRequestProperty(Expression<Func<HttpRequestBase, object>> expression)
        {
            var property = ReflectionHelper.GetProperty(expression);
            _systemProperties.Add(property);

            _requestProperties[property.Name] = expression.Compile();
        }
開發者ID:cothienlac86,項目名稱:fubumvc,代碼行數:7,代碼來源:RequestPropertyValueSource.cs

示例10: GetFriendlyName

        protected internal virtual string GetFriendlyName(Type type, Expression<Func<Type, string>> nameFunctionExpression)
        {
            if(type == null)
                throw new ArgumentNullException("type");

            if(nameFunctionExpression == null)
                throw new ArgumentNullException("nameFunctionExpression");

            if(type.IsGenericParameter)
                return type.Name;

            var name = nameFunctionExpression.Compile().Invoke(type);

            if(!type.IsGenericType)
                return name;

            name = name.Substring(0, name.IndexOf("`", StringComparison.OrdinalIgnoreCase));

            var genericArgumentFriendlyName = string.Empty;

            foreach(var genericArgument in type.GetGenericArguments())
            {
                if(!string.IsNullOrEmpty(genericArgumentFriendlyName))
                    genericArgumentFriendlyName += ", ";

                genericArgumentFriendlyName += this.GetFriendlyName(genericArgument, nameFunctionExpression);
            }

            return string.Format(CultureInfo.InvariantCulture, "{0}<{1}>", name, genericArgumentFriendlyName);
        }
開發者ID:HansKindberg-Lab,項目名稱:LINQ-Query-Provider,代碼行數:30,代碼來源:DefaultTypeExtension.cs

示例11: RouteTarget

        public RouteTarget(Expression<Func<IServiceLocator, object>> controllerExpr)
        {
            _controllerExpr = controllerExpr;
            _controllerFunc = _controllerExpr.Compile();

            ControllerType = ((_controllerExpr.Body as MethodCallExpression)?.Arguments?[0]
                as ConstantExpression)?.Value as Type;
        }
開發者ID:jammycakes,項目名稱:dolstagis.web,代碼行數:8,代碼來源:RouteTarget.cs

示例12: AddHttpMethodFilter

 public void AddHttpMethodFilter(Expression<Func<ActionCall, bool>> filter, string method)
 {
     _httpMethodFilters.Add(new HttpMethodFilter{
         Filter = filter.Compile(),
         Description = filter.Body.ToString(),
         Method = method
     });
 }
開發者ID:NeilSorensen,項目名稱:fubumvc,代碼行數:8,代碼來源:RouteConstraintPolicy.cs

示例13: NotNullOrEmpty

        public static void NotNullOrEmpty(Expression<Func<string>> expr)
        {
            if (!string.IsNullOrEmpty(expr.Compile()()))
                return;

            var param = (MemberExpression)expr.Body;
            throw new ArgumentNullException("The parameter '" + param.Member.Name + "' cannot be null or empty.");
        }
開發者ID:RussPAll,項目名稱:FhemDotNet,代碼行數:8,代碼來源:Validation.cs

示例14: FindCalls

 public IEnumerable<Call> FindCalls(Expression<Func<CallInput, bool>> filter)
 {
     if (filter == null)
     {
         throw new ArgumentNullException("filter");
     }
     var predicate = filter.Compile();
     return from call in _calls where predicate.Invoke(call.Input) select call;
 }
開發者ID:ebjornset,項目名稱:Crasop.Net,代碼行數:9,代碼來源:CallFileRepository.cs

示例15: CompileTree

 private Action<FunctionContext> CompileTree(Expression<Action<FunctionContext>> lambda)
 {
     if (this.CompileInterpretDecision == CompileInterpretChoice.AlwaysCompile)
     {
         return lambda.Compile();
     }
     int compilationThreshold = (this.CompileInterpretDecision == CompileInterpretChoice.NeverCompile) ? 0x7fffffff : -1;
     return (Action<FunctionContext>) new LightCompiler(compilationThreshold).CompileTop(lambda).CreateDelegate();
 }
開發者ID:nickchal,項目名稱:pash,代碼行數:9,代碼來源:CompiledScriptBlockData.cs


注:本文中的System.Linq.Expressions.Expression.Compile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。