Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using Eremite.Model;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace HLive
  6. {
  7.     public static class IdentifiableExtensions
  8.     {
  9.         public static T GetById<T>(this IList<T> list, int id) where T : IIdentifiable
  10.         {
  11.             int count = list.Count;
  12.             for (int i = 0; i < count; i++)
  13.                 if (list[i].Id == id)
  14.                     return list[i];
  15.  
  16.             return default;
  17.         }
  18.  
  19.         public static bool ContainsId<T>(this IList<T> list, int id) where T : IIdentifiable
  20.         {
  21.             int count = list.Count;
  22.             for (int i = 0; i < count; i++)
  23.                 if (list[i].Id == id)
  24.                     return true;
  25.  
  26.             return false;
  27.         }
  28.  
  29.         public static bool ContainsDuplicates<T>(this IList<T> list) where T : IIdentifiable
  30.         {
  31.             return list.GroupBy(x => x.Id).Any(g => g.Count() > 1);
  32.         }
  33.  
  34.         public static IEnumerable<IGrouping<int, T>> GetDuplicates<T>(this IList<T> list) where T : IIdentifiable
  35.         {
  36.             return list.GroupBy(x => x.Id).Where(g => g.Count() > 1);
  37.         }
  38.     }
  39.  
  40.     public static class IdentifiableStateExtensions
  41.     {
  42.         public static T GetByUniqueId<T>(this IList<T> list, int uniqueId) where T : IIdentifiableState
  43.         {
  44.             int count = list.Count;
  45.             for (int i = 0; i < count; i++)
  46.                 if (list[i].UniqueId == uniqueId)
  47.                     return list[i];
  48.  
  49.             return default;
  50.         }
  51.  
  52.         public static bool ContainsUniqueId<T>(this IList<T> list, int uniqueId) where T : IIdentifiableState
  53.         {
  54.             int count = list.Count;
  55.             for (int i = 0; i < count; i++)
  56.                 if (list[i].UniqueId == uniqueId)
  57.                     return true;
  58.  
  59.             return false;
  60.         }
  61.  
  62.         public static void RemoveByUniqueId<T>(this IList<T> list, int uniqueId) where T : IIdentifiableState
  63.         {
  64.             int count = list.Count;
  65.             for (int i = 0; i < count; i++)
  66.                 if (list[i].UniqueId == uniqueId)
  67.                      list.RemoveAt(i);
  68.         }
  69.  
  70.         public static T GetByModelId<T>(this IList<T> list, int modelId) where T : IIdentifiableState
  71.         {
  72.             int count = list.Count;
  73.             for (int i = 0; i < count; i++)
  74.                 if (list[i].ModelId == modelId)
  75.                     return list[i];
  76.  
  77.             return default;
  78.         }
  79.  
  80.         public static bool ContainsModelId<T>(this IList<T> list, int modelId) where T : IIdentifiableState
  81.         {
  82.             int count = list.Count;
  83.             for (int i = 0; i < count; i++)
  84.                 if (list[i].ModelId == modelId)
  85.                     return true;
  86.  
  87.             return false;
  88.         }
  89.  
  90.         public static void RemoveByModelId<T>(this IList<T> list, int modelId) where T : IIdentifiableState
  91.         {
  92.             int count = list.Count;
  93.             for (int i = 0; i < count; i++)
  94.                 if (list[i].ModelId == modelId)
  95.                     list.RemoveAt(i);
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement