English 中文(简体)
• 如何与Linq进行收集。 表达
原标题:How to query collection with Linq.Expressions
  • 时间:2012-01-16 05:21:28
  •  标签:
  • linq
  • c#-4.0

我建造了一种可贵的习俗提供者。 例如,供应商转变了询问

c.PurchaseDate == new DateTime(2011, 11, 29) && c.Name == "Elizabeth Brown"

缩略语 宣 言

现在,我需要与林吉问一起来抓他们。

IQueryable<Customer> customers = _customers.AsQueryable();

谁能告诉我如何以表达方式询问收集情况?

增 编

问题回答
//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();

我完成了这项任务。 体面确实是 wonder的。 欢乐!





相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签