Пример #1
0
 public void DeleteIfNotUsed()
 {
     if (Chats.Count < 2)
     {
         var db = new RepoContext();
         db.Remove(this);
         db.SaveChanges();
     }
 }
Пример #2
0
        public bool Exclude(ICollection <Chat> chats)
        {
            var db = new RepoContext();

            Chats.RemoveAll(x => chats.Any(y => x.ChatId == y.Id));
            if (Chats.Count < 2)
            {
                db.Remove(this);
                db.SaveChanges();
                return(true);
            }
            db.Update(this);
            db.SaveChanges();
            return(false);
        }
Пример #3
0
        public static Route Resolve(User owner, ICollection <Chat> chats)
        {
            var db = new RepoContext();

            // Ищем частичное совпадение в более чем 2 маршрутах
            var routes = db.Routes
                         .Where(x => x.Chats
                                .Any(y => chats.Contains(y.Chat) && y.Route.Owner.Id == owner.Id))
                         .ToList();

            if (routes.Any())
            {
                routes[0].Chats = routes
                                  .Select(x => x.Chats.AsEnumerable())
                                  .Aggregate((a, b) => a.Concat(b))
                                  .Distinct()
                                  .ToList();
                db.Update(routes[0]);
                foreach (var other in routes.Skip(1))
                {
                    db.Remove(other);
                }
                db.SaveChanges();
                return(routes[0]);
            }

            // Или полное совпадение
            var route = db.Routes
                        .FirstOrDefault(x => x.Chats.Select(y => y.Chat)
                                        .Intersect(chats).Count() == chats.Count);

            if (route != null)
            {
                return(route);
            }

            // Или создаем новый маршрут
            return(new Route());
        }