Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 2.79 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I use Where<T> extension method when T is unknown at compile time
  2. IEnumerable<object> NewValueSubCollection = (IEnumerable<object>)NewValue.GetType().GetProperty(col.Name).GetValue(NewValue, null);
  3. //Where col is a property of an object of type (IEnumerable<???>)
  4.  
  5. foreach (var line in ((IEnumerable<object>)col.GetValue(originalEntity, null)).ToList<object>())
  6. {
  7.     Type lineType = ObjectContext.GetObjectType(line.GetType());
  8.     var lineEntitySet = context.GetEntitySet(lineType);
  9.     EntityKey lineKey = context.CreateEntityKey(lineEntitySet.Name, line);
  10.     if (NewValueSubCollection.Where(lineKey).Count() == 0) //See bellow
  11.     context.DeleteObject(line);
  12. }
  13.        
  14. public static class LinqExtension
  15. {
  16.     //Enable searching in a collection is an entity Key exist... and retrieve info.
  17.     public static IEnumerable<T> Where<T>(this IEnumerable<T> source,EntityKey key)
  18.     {
  19.         return Enumerable.Where(source, CreateFilterExpressionBasedOnKey<T>(key).Compile());
  20.     }
  21.  
  22.     private static Expression<Func<TInput, bool>> CreateFilterExpressionBasedOnKey<TInput>(Type sourceType, EntityKey key)
  23.     {
  24.         var param = Expression.Parameter(sourceType, "");
  25.  
  26.         Expression myFilter = Expression.Constant(true);
  27.         if (key != null)
  28.         {
  29.             foreach (var item in key.EntityKeyValues)
  30.             {
  31.                 Expression Left = Expression.PropertyOrField(param, item.Key.ToString());
  32.                 Expression Right = Expression.Constant(item.Value);
  33.  
  34.                 myFilter = Expression.AndAlso(myFilter, Expression.Equal(Left, Right));
  35.             }
  36.         }
  37.         return Expression.Lambda<Func<TInput, bool>>(myFilter, param);
  38.     }
  39. }
  40.        
  41. Type generic = typeof(Collection<>);
  42. Type typeArgs = (NewValue.GetType().GetProperty(col.Name).GetValue(NewValue, null)).GetType().GetGenericArguments()[0];
  43. Type constructed = generic.MakeGenericType(typeArgs);
  44. object o = Activator.CreateInstance(constructed);
  45. o = NewValue.GetType().GetProperty(col.Name).GetValue(NewValue, null);
  46.        
  47. if (o.Where(lineKey).Count() == 0) //error!!!
  48. // 'object' does not contain a definition for 'Where' acception a first argument of type 'object' could be found (are you missing directive or an assembly reference?)
  49.        
  50. NewValueSubCollection.Where(lineKey).Count() == 0
  51.        
  52. NewValueSubCollection.Cast<dynamic>().Where(d => lineKey(d)).Count() == 0
  53.        
  54. !NewValueSubCollection.Cast<dynamic>().Any(d => lineKey(d))
  55.        
  56. class Program
  57. {
  58.     static void Main(string[] args)
  59.     {
  60.         var list = new List<string>();
  61.         IEnumerable<object> objects = list;
  62.         Func<string, bool> func = s => s.Contains("x");
  63.  
  64.         list.Add("Test");
  65.         list.Add("x");
  66.         list.Add("mux");
  67.         foreach (var item in objects.Cast<dynamic>().Where(d => func(d)))
  68.             Console.WriteLine(item);
  69.         Console.ReadKey();
  70.     }
  71. }