이의 구현은 GROUPBY 쿼리
IQueryable<Document> data = db.Documents;
ParameterExpression pe = Expression.Parameter(typeof(Document), "doc");
Expression groupBy = Expression.Call(
typeof(Queryable),
"GroupBy",
new Type[] { typeof(Document), typeof(int) },
data.Expression,
Expression.Lambda(Expression.Constant(1), pe));
ParameterExpression peg = Expression.Parameter(typeof(IGrouping<int, Document>), "group");
Expression select = Expression.Call(
typeof(Queryable),
"Select",
new Type[] { typeof(IGrouping<int, Document>), typeof(int) },
groupBy,
Expression.Lambda(Expression.Property(peg, "Key"), peg));
foreach (var item in data.Provider.CreateQuery(select)) { ... }
했다 :
db.Documents.GroupBy(a => 1).Select(b => b.Key });
완벽하게 작동합니다. 이제 그룹 키에 액세스하는 대신 합계를 집계합니다.
그것이 나를 위해 까다로운 곳입니다. 나는 이런 식으로 뭔가를 생각하고 있었다 :
IEnumerable<Document>.Sum<Document>(Func<Document, decimal> selector)
을 위해 동안 :
db.Documents.Sum(a => a.Amount)
내가 할
ParameterExpression pe1 = Expression.Parameter(typeof(Document), "other");
Expression sum = Expression.Call(
typeof(Queryable),
"Sum",
new Type[] { typeof(Document) },
peg,
Expression.Lambda(Expression.Property(pe1, "Amount"), pe1));
을 또한, Sum 함수에 대한
...b.Sum(c => c.Amount)
에 인텔리는 서명이 있습니다 :
IQueryable<Document>.Sum<Document>(Expression<Func<Document, decimal>> selector)
Selector는 한 버전에서는 Func이고 다른 버전에서는 Func입니다. Linq 표현식에서 Func 처리 방법을 모르겠습니다. 어쩌면 인텔리 센스가 틀렸을까요?
집계 소스에 대한 표현이 가장 큰 문제입니다. 보고 :
...b.Sum(c => c.Amount)
나는 b는 ('선택'의 ParameterExpression를) IGrouping해야 가정 것이고, 그 합계의 소스해야하지만, 그것은 컴파일되지 않습니다. 그 밖의 무엇을 시도 해야할지 모르겠습니까?
Expression Select = Expression.Call(
typeof(Queryable),
"Select",
new Type[] { typeof(IGrouping<int, Document>), typeof(decimal?) },
GroupBy,
Expression.Lambda(sum, peg));
하지만도 실패로 인해 '합'표현의이 지점에 도달 할 수 없습니다 여기
은 선택 식을 같이하는 방법 마지막이다.
모든 포인터가 감사하겠습니다.
감사합니다.
그냥 궁금 그룹의 포인트는 상수로 무엇 : 그와
는 샘플 쿼리 식을 만들어 보자 말했다되고? – dasblinkenlight동일한 데이터에 대해 동시에 여러 집계가 필요하므로 단일 데이터베이스를 사용하는 유일한 방법입니다. –