forked from beyond-code-github/LinqToQuerystring
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCountNode.cs
More file actions
40 lines (34 loc) · 1.46 KB
/
CountNode.cs
File metadata and controls
40 lines (34 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace LinqToQuerystring.Core.TreeNodes.Aggregates
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Antlr.Runtime;
using LinqToQuerystring.Core.TreeNodes.Base;
public class CountNode : TreeNode
{
public CountNode(Type inputType, IToken payload, TreeNodeFactory treeNodeFactory)
: base(inputType, payload, treeNodeFactory)
{
}
public override Expression BuildLinqExpression(IQueryable query, Expression expression, Expression item = null)
{
var property = this.ChildNodes.ElementAt(0).BuildLinqExpression(query, expression, item);
var underlyingType = property.Type;
if (typeof(IEnumerable).IsAssignableFrom(property.Type) && property.Type.GetGenericArguments().Any())
{
underlyingType = property.Type.GetGenericArguments()[0];
}
else
{
//We will sometimes need to cater for special cases here, such as Enumerating BsonValues
underlyingType = Context.EnumerableTypeMap(underlyingType);
var enumerable = typeof(IEnumerable<>).MakeGenericType(underlyingType);
property = Expression.Convert(property, enumerable);
}
return Expression.Call(typeof(Enumerable), "Count", new[] { underlyingType }, property);
}
}
}