0/5 (1 avis)
Snippet vu 7 877 fois - Téléchargée 20 fois
// ExpressionExtensions.cs public static class ExpressionExtensions { public static String GetPropertyName<T, TProperty>(this Expression<Func<T, TProperty>> expression) { return GetPropertyName(expression); } public static String GetPropertyName(this Expression expression) { LambdaExpression lambdaExpression = expression as LambdaExpression; if (lambdaExpression != null) { expression = lambdaExpression.Body; } MemberExpression memberExpression = expression as MemberExpression; if (memberExpression != null) { String parentPropertyName = String.Empty; if (memberExpression.Expression is MemberExpression) { parentPropertyName = ExpressionExtensions.GetPropertyName(memberExpression.Expression) + "."; } return parentPropertyName + memberExpression.Member.Name; } throw new NotImplementedException(); } } // ObjectQueryExtensions.Include.tt <#@ template debug="true" hostSpecific="true" #> <#@ output extension=".cs" #> <#@ Assembly Name="System.Core.dll" #> <#@ Assembly Name="System.Windows.Forms.dll" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Diagnostics" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.Collections.Generic" #> <# Byte maxDepth = 6; #> using System; using System.Collections.Generic; using System.Data.Objects; using System.Data.Objects.DataClasses; using System.Linq; using System.Linq.Expressions; namespace Magelia.WebStore.Data.Entities { public static partial class ObjectQueryExtensions { internal static String GetPath(params LambdaExpression[] lambdas) { return String.Join(".", lambdas .Select(l => l.GetPropertyName()) .ToArray() ); } <# for(Byte i = 1; i < maxDepth; i++) { int combinationCount = (int)Math.Pow(2, i); for (int j = 0; j < combinationCount; j++) { WriteMethod(i, j); } } #> } } <#+ void WriteMethod(Byte depth, int combinationValue) { String[] genericsType = new String[depth+1]; genericsType[0] = "TEntity"; int i = depth-1; while (i >= 0) { String genericType = "T" + i.ToString(); if ((combinationValue & (1 << i)) != 0) { genericType = "IEnumerable<" + genericType + ">"; } genericsType[i+1] = genericType; i--; } String[] parametersType = new String[depth]; for(i = 0; i < depth; i++) { parametersType[i] = String.Format("Expression<Func<T{0}, {1}>> selector{2}", (i == 0) ? "Entity" : (i-1).ToString(), genericsType[i+1], i); } #> public static ObjectQuery<TEntity> Include<TEntity, <#= String.Join(", ", Enumerable.Range(0, depth).Select(j => "T" + j.ToString()).ToArray()) #>>(this ObjectQuery<TEntity> objectSet, <#= String.Join(", ", parametersType) #>) where TEntity : class { return objectSet.Include(GetPath(<#= String.Join(", ", Enumerable.Range(0, depth).Select(j => "selector" + j.ToString()).ToArray()) #>)); } <#+ } #>
25 sept. 2012 à 14:51
Ici, on est chez les newbies des prairies et on tient à y rester ! Alors les gens qui se la pètent avec leur code trop compliqués, on en veut pas hein ;)
Allez, dégage l'expert et laisse nous à nos éditeurs de textes, compteurs de lettres, et autres softwares de-la-mort-qui-tue.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.