Extensions.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. namespace DymWebForm.Common.Linq
  7. {
  8. internal static class Extensions
  9. {
  10. public static T2[] SelectArray<T1, T2>(this IEnumerable<T1> source, Func<T1, T2> selector)
  11. {
  12. return source.Select(selector).ToArray();
  13. }
  14. public static T2[] SelectArray<T1, T2>(this IQueryable<T1> source, Expression<Func<T1, T2>> selector)
  15. {
  16. return source.Select(selector).ToArray();
  17. }
  18. public static T2 FirstValue<T1, T2>(this Dictionary<T1, T2> source)
  19. {
  20. foreach (var item in source)
  21. {
  22. return item.Value;
  23. }
  24. throw new InvalidOperationException("No items in dic");
  25. }
  26. public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> proc)
  27. {
  28. var arr = source as T[] ?? source.ToArray();
  29. foreach (var item in arr)
  30. proc(item);
  31. return arr;
  32. }
  33. public static IEnumerable<T> ForEach<T>(this IQueryable<T> source, Action<T> proc)
  34. {
  35. foreach (var item in source)
  36. proc(item);
  37. return source;
  38. }
  39. }
  40. }