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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Linq.Expressions;
using System.Threading;
using System.Reflection.Emit;
using System.Collections;
using System.Collections.Specialized;
namespace GenerationExpressionGroupBy
{
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
static class Program
{
static void Main(string[] args)
{
List<Pet> petsList =
new List<Pet>{ new Pet { Name="Bernard", Age=8.3 },
new Pet { Name="Boots", Age=4.9 },
new Pet { Name="Boots", Age=1.5 },
new Pet { Name="Toto", Age=4.3 } };
Type T = typeof(Pet);
Dictionary<string, PropertyInfo> sourceProperties = new Dictionary<string, PropertyInfo>();
sourceProperties.Add("Name", T.GetProperty("Name"));
sourceProperties.Add("Sum(Age)", T.GetProperty("Age"));
IEnumerable<object> PetsListObject = petsList;
IEnumerable<object> Resultat = PetsListObject.AsQueryable().GroupDynamic(sourceProperties) as IEnumerable<object>;
// Je souhaiterai un equivalent
var query = petsList.GroupBy(
pet => pet.Name,
(name, pets) => new
{
Name = name,
Count = pets.Count(),
Min = pets.Min(pet => pet.Age),
Max = pets.Max(pet => pet.Age),
TotalAge = pets.Sum(pet => pet.Age)
});
}
static string testFunction(string table, out string opera)
{
opera = "";
if (table.ToLower().Contains("sum(") || table.ToLower().Contains("min(") || table.ToLower().Contains("lower(") || table.ToLower().Contains("upper(") || table.ToLower().Contains("max(") || table.ToLower().Contains("count(") || table.ToLower().Contains("avg("))
{
int longueur = "sum".Length;
opera = table.Substring(0, longueur);
if (opera.ToLower().Equals("cou"))
{
opera += "nt";
longueur += 2;
}
if (opera.ToLower().Equals("upp") || opera.ToLower().Equals("low"))
{
opera += "er";
longueur += 2;
}
longueur++;
table = table.Substring(longueur, table.Length - (longueur));
table = table.Trim(')');
}
return table;
}
static IQueryable GroupDynamic(this IQueryable source, Dictionary<string, PropertyInfo> selectDictionnary)
{
MethodInfo methode = typeof(PropertyInfo).GetMethod("GetValue", new System.Type[] { typeof(object), typeof(object[]) });
object[] vide = new object[0];
Type dynamicType = LinqRuntimeTypeBuilder.GetDynamicType(selectDictionnary.Values);
ParameterExpression sourceItem = Expression.Parameter(source.ElementType, "clef");
ParameterExpression sourceFunctionItem = Expression.Parameter(typeof(IEnumerable<object>), "cible");
// Genere l'expression dynamique du 2eme argument du group by
IEnumerable<MemberBinding> bindings = dynamicType.GetFields().Select(p => getBinding(p, selectDictionnary, sourceItem, sourceFunctionItem)).OfType<MemberBinding>();
// Compilation de l'expression dynamique
var selector2 = Expression.Lambda(Expression.MemberInit(
Expression.New(dynamicType.GetConstructor(Type.EmptyTypes)), bindings), sourceItem, sourceFunctionItem);
var predicat = selector2.Compile();
// Intregration de l'expression au group by
var query = (source as IEnumerable<object>).GroupBy<Func<object, object>>(
getExpression(typeof(Pet).GetProperty("Age"), sourceItem),
predicat);
/// EXEMPLE DE REQUETE CONTENU DANS LE PREDICAT
//(name, pets) => new
//{
// Name = name,
// Count = pets.Count(),
// Min = pets.Min(pet => pet.Age),
// Max = pets.Max(pet => pet.Age),
// TotalAge = pets.Sum(pet => pet.Age)
//}
return source.Provider.CreateQuery(query);
}
static MemberAssignment getBinding(FieldInfo p, Dictionary<string, PropertyInfo> sourceProperties, ParameterExpression sourceItem, ParameterExpression sourceFunctionItem)
{
string opera;
string val;
foreach (KeyValuePair<string, PropertyInfo> entre in sourceProperties)
{
if ((val = testFunction(entre.Key, out opera)).Equals(p.Name) && !opera.Equals("") && p.Name.Contains(val))
{
return Expression.Bind(p, getExpressionComplexe(opera, sourceProperties[entre.Key], sourceItem, sourceFunctionItem));
}
else if (val.Equals(p.Name) && opera.Equals(""))
return Expression.Bind(p, getExpression(sourceProperties[p.Name], sourceItem));
}
throw new Exception("erreur de references");
}
// Min = pets.Min(pet => pet.Age),
// Max = pets.Max(pet => pet.Age),
private static Expression getExpressionComplexe(string opera, PropertyInfo P, ParameterExpression sourceItem, ParameterExpression sourceFunctionItem)
{
MethodInfo methode = typeof(PropertyInfo).GetMethod("GetValue", new System.Type[] { typeof(object), typeof(object[]) });
object[] vide = new object[0];
Expression E = Expression.Convert(Expression.Call(
Expression.Constant(P),
methode,
sourceItem,
Expression.Constant(vide)), typeof(double));
var lambda = Expression.Lambda<Func<object, double>>(E, sourceItem);
var methodeFunctionGenericsList = typeof(Enumerable).GetMethods().Where(d => d.Name.ToLower().Contains(opera.ToLower()) && d.IsGenericMethod);
var methodeFunctionGeneric = methodeFunctionGenericsList.Where(d => d.GetParameters().Any(param => param.Position == 1 && param.ToString().Contains(P.PropertyType.ToString().Split('.')[1]))).FirstOrDefault();
var methodeFunctionDouble = methodeFunctionGeneric.MakeGenericMethod(new[] { typeof(object) });
Type T = E.Type;
Expression function = Expression.Call(null, methodeFunctionDouble, sourceFunctionItem, lambda);
return function;
}
static Expression getExpression(PropertyInfo P, ParameterExpression sourceItem)
{
MethodInfo methode = typeof(PropertyInfo).GetMethod("GetValue", new System.Type[] { typeof(object), typeof(object[]) });
object[] vide = new object[0];
return Expression.Convert(Expression.Call(
Expression.Constant(P),
methode,
sourceItem,
Expression.Constant(vide)), getType(P));
}
static Type getType(PropertyInfo P)
{
return P.PropertyType;
}
}
static class LinqRuntimeTypeBuilder
{
//private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static AssemblyName assemblyName = new AssemblyName() { Name = "DynamicLinqTypes" };
private static ModuleBuilder moduleBuilder = null;
private static Dictionary<string, Type> builtTypes = new Dictionary<string, Type>();
static LinqRuntimeTypeBuilder()
{
moduleBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run).DefineDynamicModule(assemblyName.Name);
}
private static string GetTypeKey(Dictionary<string, Type> fields)
{
string key = string.Empty;
foreach (var field in fields)
key += field.Key + ";" + field.Value.Name + ";";
return key;
}
public static Type GetDynamicType(Dictionary<string, Type> fields)
{
if (null == fields)
throw new ArgumentNullException("fields");
if (0 == fields.Count)
throw new ArgumentOutOfRangeException("fields", "fields must have at least 1 field definition");
try
{
Monitor.Enter(builtTypes);
string className = GetTypeKey(fields);
if (builtTypes.ContainsKey(className))
return builtTypes[className];
TypeBuilder typeBuilder = moduleBuilder.DefineType(className, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Serializable);
foreach (var field in fields)
typeBuilder.DefineField(field.Key, field.Value, FieldAttributes.Public);
builtTypes[className] = typeBuilder.CreateType();
return builtTypes[className];
}
catch (Exception ex)
{
//log.Error(ex);
throw new Exception("erreur");
}
finally
{
Monitor.Exit(builtTypes);
}
return null;
}
private static string GetTypeKey(IEnumerable<PropertyInfo> fields)
{
return GetTypeKey(fields.ToDictionary(f => f.Name, f => f.PropertyType));
}
public static Type GetDynamicType(IEnumerable<PropertyInfo> fields)
{
return GetDynamicType(fields.ToDictionary(f => f.Name, f => f.PropertyType));
}
}
} |
Partager