forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionHelper.cs
More file actions
43 lines (37 loc) · 1.85 KB
/
ExpressionHelper.cs
File metadata and controls
43 lines (37 loc) · 1.85 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
41
42
43
using System.Collections.Generic;
using System.Linq;
namespace Simple.Data
{
public class ExpressionHelper
{
public static SimpleExpression CriteriaDictionaryToExpression(string tableName, IEnumerable<KeyValuePair<string, object>> dictionary)
{
if (dictionary.Count() == 1)
{
return CriteriaPairToExpression(tableName, dictionary.Single());
}
return new SimpleExpression(CriteriaPairToExpression(tableName, dictionary.First()),
CriteriaDictionaryToExpression(tableName, dictionary.Skip(1)),
SimpleExpressionType.And);
}
public static SimpleExpression CriteriaDictionaryToExpression(ObjectReference table, IEnumerable<KeyValuePair<string, object>> dictionary)
{
var list = dictionary.ToList();
if (list.Count == 1)
{
return CriteriaPairToExpression(table, list[0]);
}
return new SimpleExpression(CriteriaPairToExpression(table, list[0]),
CriteriaDictionaryToExpression(table, list.Skip(1)),
SimpleExpressionType.And);
}
private static SimpleExpression CriteriaPairToExpression(string tableName, KeyValuePair<string, object> pair)
{
return new SimpleExpression(new ObjectReference(pair.Key, new ObjectReference(tableName)), pair.Value, SimpleExpressionType.Equal);
}
private static SimpleExpression CriteriaPairToExpression(ObjectReference table, KeyValuePair<string, object> pair)
{
return new SimpleExpression(new ObjectReference(pair.Key, table), pair.Value, SimpleExpressionType.Equal);
}
}
}