using System.Collections.Generic; using System.Linq; namespace Learn.Models; public class ItemGroup(IEnumerable items) { public IEnumerable GetKeys() { IEnumerable inter = null; foreach (var item in items) { if (inter == null) { inter = item.Info.Keys; } else { inter = inter.Intersect(item.Info.Keys); } } if(inter == null) return new List(); return inter.ToList(); } public bool TryGetValue(string key, out string value, out bool isSame) { value = null; isSame = true; if (!GetKeys().Contains(key)) return false; bool isFirst = true; foreach (var item in items) { if (isFirst) { isFirst = false; } else { isSame = value == item.Info[key]; if(!isSame) return true; } value = item.Info[key]; } return true; } public void SetValue(string key, string value) { foreach (var item in items) { item.Info[key] = value; } } public bool Remove(string key) { if (!GetKeys().Contains(key)) return false; foreach (var item in items) { item.Info.Remove(key); } return true; } private bool AnyContains(string key) { return items.Any(item => item.Info.Keys.Contains(key)); } public bool TryAdd(string key, string value) { if (AnyContains(key)) return false; foreach (var item in items) { item.Info.Add(key, value); } return true; } }