나는 내 목록 컬렉션에 동적 프로젝트를 적용하려고합니다. 사용자는 주목할만한 항목이지만 내 목록의 속성을 선택할 것입니다. LINQ 문에서 열의 하위 집합을 가져 오려고합니다. Dynamic LINQ를 사용하고 싶습니다. any1은 표현식 트리로 프로젝트를 구현하는 방법을 제공 할 수 있습니다.LINQ를 통해 목록에 식 트리 적용
재산권 XXXX이 유형에 대해 정의되지 않은 경우, '나는 속성을 읽는 동안, 그것은 예외를 throw 작동하지 않는 경우,
내가 코드를 아래에 구현하려고 ... 푸른 테이블 저장 동적 투사을 적용 할 선택 System.String '
코드 : 당신은 당신이 모델의 클래스 정의를 게시하는 경우
DataMovementDataContext dbMovement = new DataMovementDataContext();
var entity = dbMovement.ListofAccountingDocs2_1075s.AsQueryable();
Type type = entity.ElementType;
var entityParam = Expression.Parameter(entity.ElementType, "row");
Expression expr = entityParam;
string[] props = "AccountingDocumentNbr,GLCompanyCode,DocumentFiscalYearNbr".Split(',');
var epr = GenerateMemberExpression<ListofAccountingDocs2_1075, string>("Name");
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
// type = pi.PropertyType; //Property 'System.String GLCompanyCode' is not defined for type 'System.String'
}
// row => row.Property
// var columnLambda = Expression.Lambda( Expression.Property(entityParam, "GLCompanyCode"), entityParam);
var columnLambda = Expression.Lambda(Expression.Property(expr, "AccountingDocumentNbr,GLCompanyCode"), entityParam);
// Items.Select(row => row.Property)
var selectCall = Expression.Call(typeof(Queryable), "Select", new Type[] { entity.ElementType, columnLambda.Body.Type }, entity.Expression, columnLambda);
// Items.Select(row => row.Property).Distinct
var distinctCall = Expression.Call(typeof(Queryable), "Distinct", new Type[] { typeof(string) }, selectCall);
// colvalue => colvalue
var sortParam = Expression.Parameter(typeof(string), "AccountingDocumentNbr");
var columnResultLambda = Expression.Lambda(sortParam, sortParam);
// Items.Select(row => row.Property).Distinct.OrderBy(colvalue => colvalue)
var ordercall = Expression.Call(typeof(Queryable), "OrderBy",
new Type[] { typeof(string), columnResultLambda.Body.Type },
distinctCall, columnResultLambda);
var result = entity.Provider.CreateQuery(ordercall);
foreach (var item in result)
{
Console.Write(item);
}
"작동하지 않습니다."에 대해 자세히 설명하십시오. –
내 게시물을 편집했습니다. 투표 상태를 변경하십시오. – user145610