기본적으로 표현식 트리를 만들어야합니다. 다행히도 Expression.Property
을 사용하는 것은 그리 어렵지 않습니다. 그것을 Queryable.Where
으로 전달하거나 컴파일하여 Enumerable.Where
으로 전달하십시오. (물론 당신은 당신이 만들려고 노력하고 비교의 종류에 따라,뿐만 아니라 Expression.Equal
같은 것을 사용해야합니다.)
는 CompValue
는 실제 값을 될 운명인가? TypeOfCompare
은 무엇을 의미합니까?
LINQ to Entities가이 항목에 적합한 지 잘 모르겠습니다 ... LINQ to Objects 만 사용하고 있습니다.
편집 : 좋아요, 여기 샘플이 있습니다. 당신이 평등을 원한다고 가정하지만, 그렇게한다면 당신이 원하는 것을합니다. 대신 문자열 ColumnName
의
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
static class Extensions
{
public static List<T> Filter<T>
(this List<T> source, string columnName,
string compValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "x");
Expression property = Expression.Property(parameter, columnName);
Expression constant = Expression.Constant(compValue);
Expression equality = Expression.Equal(property, constant);
Expression<Func<T, bool>> predicate =
Expression.Lambda<Func<T, bool>>(equality, parameter);
Func<T, bool> compiled = predicate.Compile();
return source.Where(compiled).ToList();
}
}
class Test
{
static void Main()
{
var people = new[] {
new { FirstName = "John", LastName = "Smith" },
new { FirstName = "John", LastName = "Noakes" },
new { FirstName = "Linda", LastName = "Smith" },
new { FirstName = "Richard", LastName = "Smith" },
new { FirstName = "Richard", LastName = "Littlejohn" },
}.ToList();
foreach (var person in people.Filter("LastName", "Smith"))
{
Console.WriteLine(person);
}
}
}
오른쪽 ... Linq to Objects ... 우리는 (firstname, lastname, phone)과 같은 데이터를 포함하는 클래스가 있으며 CompValue는 우리가 찾고있는 실제 값입니다 match ... 그래서 예제는 ... personCollection = personCollection.Filter ("LastName", "Smith"); 그리고 우리는 스미스의 "필터링 된"컬렉션을 반환 할 것입니다 ... 감사합니다 –
... 감사합니다 존. John K. –