Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Linq.表达式查询集合_Linq_C# 4.0 - Fatal编程技术网

如何使用Linq.表达式查询集合

如何使用Linq.表达式查询集合,linq,c#-4.0,Linq,C# 4.0,我构建了一个自定义IQueryable提供程序。例如,提供者转换查询 c.PurchaseDate == new DateTime(2011, 11, 29) && c.Name == "Elizabeth Brown" 从基础代码到System.Linq.Expressions.Expression 现在,我需要使用Linq查询对这个集合运行它们 IQueryable<Customer> customers = _customers.AsQueryable();

我构建了一个自定义IQueryable提供程序。例如,提供者转换查询

c.PurchaseDate == new DateTime(2011, 11, 29) && c.Name == "Elizabeth Brown"
从基础代码到System.Linq.Expressions.Expression

现在,我需要使用Linq查询对这个集合运行它们

IQueryable<Customer> customers = _customers.AsQueryable();
有人能告诉我如何用表达式查询集合吗

谢谢


我就这样完成了任务。IQueryable真是太棒了。享受吧

哇!这真令人印象深刻!行表达式left=Expression.Propertype,typeofCustomer应该有参数而不是pe right?实际上,在您的实际代码中,您必须重写一个抽象ExpressionVisitor类才能在运行时访问和挤出这些值,但为了演示,我在静态上下文中创建了这三个表达式值。是的,你是对的。我在准备样品时犯了一个错误,但现在已经纠正了。此代码仅表示基本功能,只是为了了解此技术的工作原理。为了证明我创建了lambda子句的左表达式和右表达式,对应于c.PurchaseDate==new DateTime2011、11、29和&c.Name==Elizabeth Brown,ParameterExpression参数只定义了类型和参数c本身。
//Query = c.PurchaseDate == new DateTime(2011, 11, 29) && c.Name
    // == "Elizabeth Brown" )
IQueryable<Customer> customers = _customers.AsQueryable<Customer>();

//Predicate parameter
ParameterExpression parameter = Expression.Parameter(typeof(Customer), 
                                                          "customer");

//Create left expression
Expression left = Expression.Property(parameter, typeof(Customer)
                                     .GetProperty("PurchaseDate"));
Expression right = Expression.Constant(new DateTime(2011, 11, 29));
Expression leftExp = Expression.Equal(left, right);

//Create right expression tree
left = Expression.Property(parameter, typeof(Customer).GetProperty("Name"));
right = Expression.Constant("Elizabeth Brown", typeof(string));
Expression rightExp = Expression.Equal(left, right);

//Combine the expressions into expression tree
Expression expressionTree = Expression.AndAlso(leftExp, rightExp);

//Create an expression tree that represents the expression
MethodCallExpression methodCall = Expression.Call(
    typeof(Queryable),
    "Where",
    new Type[] { customers.ElementType },
    customers.Expression,
    Expression
               .Lambda<Func<Customer, bool>>
                 (expressionTree, new ParameterExpression[] { parameter }));

// Create an executable query from the expression tree.
IQueryable<Customer> results = 
                customers.Provider.CreateQuery<Customer>(methodCall);

// Enumerate the results
foreach (Customer customer in results)
{
    Console.WriteLine("{0} {1}", customer.Name, customer.PurchaseDate);
}

Console.ReadLine();