There is a class diagram

public class Particip { public string Name { get; set; } public List<Result> Results { get; set; } = new List<Result>(); } public class Result { public int SubjectCode { get; set; } //1-русский язык, 2-математика, 3-физика public int Mark5 { get; set; } //отметка по 5-ти бальной шкале } class Program { static void Main(string[] args) { var participResults1 = new List<Result> { new Result { SubjectCode = 1, Mark5 = 2 }, new Result { SubjectCode = 2, Mark5 = 2 }, new Result { SubjectCode = 3, Mark5 = 3 } }; var participResults2 = new List<Result> { new Result { SubjectCode = 1, Mark5 = 3 }, new Result { SubjectCode = 2, Mark5 = 3 }, new Result { SubjectCode = 3, Mark5 = 2 } }; var particips = new List<Particip> { new Particip { Name = "Иван", Results = participResults1 }, new Particip { Name = "Андрей", Results = participResults2 } }; Console.ReadLine(); } } 

Here participants of testing and their results in subjects are described.


Question

  1. It is necessary to find out: are there any particips in the particips collection who have only 2 Mark5 == 2 on one subject ( Mark5 == 2 ).
  2. And change that number to 3.

Those. In this example, this participant is Andrew - he has only one deuce (in physics) and we need to update the particips collection with the editing of this deuce by three.


Attempts

I swear I tried various versions of the Any , Where and Count methods and nothing happened. I did not try GroupBy - because of the nesting of the collection, I didn’t know which side would be suitable.

  • MSDN: Enumerable . Single . Although no :-) question does not match the title. And although there is no - it is like - Grundy
  • I swear I tried various options of methods - add options (code) that I tried, with a description of what exactly does not fit - Grundy
  • @Grundy add options (code) that you tried, with a description of what exactly does not fit . Thank you for your comment. I will definitely consider this in my future posts. - Adam
  • You can add them to this question using the edit button for editing - Grundy

2 answers 2

Hint: You can also work with Results using LINQ.

For example:

 var matched = particips .Where(p => p.Results .Where(r => r.Mark5 == 2) //Получаем список результатов, равных 2. .Count() == 1); //Проверяем, что для текущего элемента есть только один такой результат 

Thus, we have the desired list of losers who can be forgiven.

The forgiveness procedure for Losers will look something like this:

 foreach (var item in matched) { //Мы уже знаем, что для каждого элемента есть только один нужный результат, //так что можно не париться: item.Results.First(r => r.Mark5 == 2).Mark5 = 3; } 
  • You can .First(r => r.Mark5 == 2) add to the end of the first LINQ - RusArt
  • @RuslanArtamonov Well, the author asked two questions, I gave two answers. This can be done without a second query at all, since these are reference types - eastwing
  • @eastwing, how without a second query? - Grundy
  • @Grundy, for example, in the first request with a selector, collect the necessary elements in a sequence, and then work with it without a request. - eastwing
 particips.Select(p => new { Particip = p, Mark5_2 = p.Results.Where(r => r.Mark5 == 2).ToList() }) .Where(pm => pm.Mark5_2.Count == 1) .ToList() .ForEach(pm => pm.Mark5_2[0].Mark5 = 3);