From e603470fdb5d73a02484babd35fedafb6f7df232 Mon Sep 17 00:00:00 2001 From: Abdurahmon Date: Fri, 18 Aug 2023 11:46:25 +0500 Subject: [PATCH] uy ishi N26 buldi --- N26_HT2/Extention/CollectionExtensions.cs | 34 ++++++++++++++++++-- N26_HT2/Program.cs | 39 +++++++++++++++++++++++ N26_HT2/Skills.cs | 6 ++-- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/N26_HT2/Extention/CollectionExtensions.cs b/N26_HT2/Extention/CollectionExtensions.cs index e3d6ae9..36383c5 100644 --- a/N26_HT2/Extention/CollectionExtensions.cs +++ b/N26_HT2/Extention/CollectionExtensions.cs @@ -1,6 +1,36 @@ namespace N26_HT2.Extention; -public class CollectionExtensions +public static class CollectionExtensions { - + + public static ICollection Update(this ICollection firstcollection, + ICollection secondcollection) + { + var list = firstcollection.ToList(); + + var addedItems = secondcollection + .ExceptBy(firstcollection.Select(firstItem => firstItem.Id), item => item.Id); + + var removedItems = firstcollection + .ExceptBy(secondcollection.Select(firstItem => firstItem.Id), item => item.Id); + + var intersectKeys = firstcollection.Select(item => item.Id) + .Intersect(secondcollection.Select(item => item.Id)); + + foreach (var item in addedItems) + list.Add(item); + + foreach (var item in removedItems) + list.Remove(item); + + foreach (var key in intersectKeys) + { + var firstItem = list.First(a => a.Id == key); + var secondItem = secondcollection.First(a => a.Id == key); + + firstItem.Name = secondItem.Name; + firstItem.Level = secondItem.Level; + } + return list; + } } \ No newline at end of file diff --git a/N26_HT2/Program.cs b/N26_HT2/Program.cs index b1556f2..162b424 100644 --- a/N26_HT2/Program.cs +++ b/N26_HT2/Program.cs @@ -15,3 +15,42 @@ //- added - qo'shilganlar ( second da bor, first da yo'q ) -bularni first ga qo'shish kerak //- removed - o'chirilganlar ( second da yo'q, first da bor ) -bularni first dan o'chirish kerak //- updated - o'zgartirilgan ( first da va second da bor va o'zgartirilgan ) -bularni first da update qilish kerak ( id dan tashqari ) + +using System.Text.Json; +using N26_HT2; +using N26_HT2.Enums; +using N26_HT2.Extention; + + +var skillsA = new List +{ + new("Vue", SkillLevels.Beginner), + new("Angular", SkillLevels.Experienced), + new("React", SkillLevels.Master), + new(".NET", SkillLevels.Expert), + new("Python", SkillLevels.Beginner), +}; + +Console.WriteLine(JsonSerializer.Serialize(skillsA)); +Console.WriteLine(); + +var updatedItemA = skillsA[2]; +updatedItemA.Name = "Node.js"; +var updatedItemB = skillsA[2]; +updatedItemB.Level = SkillLevels.Beginner; + +var skillB = new List +{ + new("Ruby", SkillLevels.Experienced), + new("Swift", SkillLevels.Expert), + updatedItemA, + updatedItemB, + skillsA[0], + skillsA[3], +}; + +Console.WriteLine(JsonSerializer.Serialize(skillB)); +Console.WriteLine(); + +var result = skillsA.Update(skillB); +Console.WriteLine(JsonSerializer.Serialize(result)); diff --git a/N26_HT2/Skills.cs b/N26_HT2/Skills.cs index 3272e1c..6214df1 100644 --- a/N26_HT2/Skills.cs +++ b/N26_HT2/Skills.cs @@ -10,13 +10,13 @@ namespace N26_HT2 public class Skills { //- Skill modelidan foydalaning(id, name, level ) - public int Id { get; set; } + public Guid Id { get; set; } public string Name { get; set; } public SkillLevels Level { get; set; } - public Skills(int id,string name, SkillLevels lavel) + public Skills(string name, SkillLevels lavel) { - Id = id; + Id = Guid.NewGuid(); Name = name; Level = lavel; }