LinqExtensions.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. ////----------------------------------------------------------------
  6. ////Module Name: LinqExtensions
  7. ////Purpose:
  8. ////CopyRight: Muchinfo
  9. ////History:
  10. ////----------------------------------------------------------------
  11. ////DateTime 2015/1/20 11:19:51
  12. ////Author ouyang.hongbin
  13. ////Description Create
  14. ////----------------------------------------------------------------
  15. //----------------------------------------------------------------
  16. //Module Name: LinqExtensions
  17. //Purpose:
  18. //CopyRight: Muchinfo
  19. //History:
  20. //----------------------------------------------------------------
  21. //DateTime 2015/1/20 11:19:51
  22. //Author ouyang.hongbin
  23. //Description Create
  24. //----------------------------------------------------------------
  25. namespace Muchinfo.MTPClient.Data.Extensions
  26. {
  27. public static class LinqExtensions
  28. {
  29. public static IEnumerable<TSource> Distinct<TSource, TProperty>(this IEnumerable<TSource> source, Func<TSource, TProperty> selector)
  30. {
  31. if (source == null)
  32. {
  33. throw new ArgumentNullException("source");
  34. }
  35. if (selector == null)
  36. {
  37. throw new ArgumentNullException("selector");
  38. }
  39. var pdis = source.Select(t => selector(t)).Distinct();
  40. foreach (var item in pdis)
  41. {
  42. yield return source.First(t => selector(t).Equals(item));
  43. }
  44. }
  45. }
  46. }