2010-03-24 1 views
4

업데이트누군가가 관심이 있다면 마크의 도움말을 표현 나무에게 AlphaPagedList 클래스 <a href="http://alphapagedlist.codeplex.com/" rel="nofollow noreferrer" title="is now available on CodePlex">is now available on CodePlex</a>을

감사를 사용하여 LINQ 쿼리를 생성

나에 대한 식 트리를 만들려고 해요

원래 주어진 charecter로 시작하는 요소들을 반환한다.

IList<char> chars = new List<char>{'a','b'}; 
IQueryable<Dept>Depts.Where(x=> chars.Contains(x.DeptName[0])); 

은 내가 예에서 선택 속성에 lamdba를 제공하는 곳은 어떤 IEnumerable을 사용할 수 원하는 :

Depts.Alpha(x=>x.DeptName, chars);

나는이 시도하지만, 전혀 운이없는 봤는데 , 어떤 도움?

public static IQueryable<T> testing<T>(this IQueryable<T> queryableData, Expression<Func<T,string>> pi, IEnumerable<char> chars) 
{ 
// Compose the expression tree that represents the parameter to the predicate. 

ParameterExpression pe = Expression.Parameter(queryableData.ElementType, "x"); 
ConstantExpression ch = Expression.Constant(chars,typeof(IEnumerable<char>)); 
// ***** Where(x=>chars.Contains(x.pi[0])) ***** 
// pi is a string property 
//Get the string property 

Expression first = Expression.Constant(0); 
//Get the first character of the string 
Expression firstchar = Expression.ArrayIndex(pi.Body, first); 
//Call "Contains" on chars with argument being right 
Expression e = Expression.Call(ch, typeof(IEnumerable<char>).GetMethod("Contains", new Type[] { typeof(char) }),firstchar); 


MethodCallExpression whereCallExpression = Expression.Call(
    typeof(Queryable), 
    "Where", 
    new Type[] { queryableData.ElementType }, 
    queryableData.Expression, 
    Expression.Lambda<Func<T, bool>>(e, new ParameterExpression[] { pe })); 
// ***** End Where ***** 

return (queryableData.Provider.CreateQuery<T>(whereCallExpression)); 
} 

답변

2

(다시 읽기 질문 후 편집) 같은 뭔가 - Expression.Invoke가 3.5SP1에 EF에서 작동하지 않습니다하지만 (하지만이 글은 괜찮 LINQ - 투 - SQL) :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Linq.Expressions; 

class Dept 
{ 
    public string DeptName { get; set; } 
} 
public static class Program 
{ 
    static void Main() 
    { 
     IList<char> chars = new List<char>{'a','b'}; 
     Dept[] depts = new[] { new Dept { DeptName = "alpha" }, new Dept { DeptName = "beta" }, new Dept { DeptName = "omega" } }; 
     var count = testing(depts.AsQueryable(), dept => dept.DeptName, chars).Count(); 
    } 

    public static IQueryable<T> testing<T>(this IQueryable<T> queryableData, Expression<Func<T,string>> pi, IEnumerable<char> chars) 
    { 
     var arg = Expression.Parameter(typeof(T), "x"); 
     var prop = Expression.Invoke(pi, arg); 
     Expression body = null; 
     foreach(char c in chars) { 
      Expression thisFilter = Expression.Call(prop, "StartsWith", null, Expression.Constant(c.ToString())); 
      body = body == null ? thisFilter : Expression.OrElse(body, thisFilter); 
     } 
     var lambda = Expression.Lambda<Func<T, bool>>(body ?? Expression.Constant(false), arg); 
     return queryableData.Where(lambda); 
    } 
} 
+0

완벽한 덕분입니다. 나는 (IPagedList의 라인을 따라) 영숫자 페이지리스트에 대해 작업하고 있는데 이것은 값을 헤아릴 수 없을 것이다. – Chao