本文整理汇总了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);
}
}
示例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();
}
示例3: Find
public IEnumerable<User> Find(Expression<Func<User, bool>> where)
{
using (var db = new UserContext())
{
return db.Users.Where(where.Compile()).ToArray();
}
}
示例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);
}
示例5: AssertJiraHttpLink
private void AssertJiraHttpLink(Expression<Func<IHttpInterfaceConfiguration, string>> configPropertyAccessor)
{
IHttpInterfaceConfiguration httpConfig = JiraConfig.Instance.HttpInterfaceConfiguration;
Assert.AreEqual(
ExpectedUrlFrom(configPropertyAccessor),
configPropertyAccessor.Compile().Invoke(httpConfig));
}
示例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)));
}
}
示例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;
}
示例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;
}
示例9: AddRequestProperty
public static void AddRequestProperty(Expression<Func<HttpRequestBase, object>> expression)
{
var property = ReflectionHelper.GetProperty(expression);
_systemProperties.Add(property);
_requestProperties[property.Name] = expression.Compile();
}
示例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);
}
示例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;
}
示例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
});
}
示例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.");
}
示例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;
}
示例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();
}