I get suppose such data

id name value 1 name 1 2 name 2 3 name2 NaN 4 name2 NaN 5 name3 10 6 name4 NaN 

I want to get an array of all names where the value is NaN. if I run through the list and do add, I get doubles. how to get a set of unique names and id

that is what I want to get

  array = ['name2' => [3,4], 'name4' => [6] ] 

so it turns out I am overwriting aydishniki

  var NonValueArray = new Dictionary<string, double>(); for (int i = 0, cnt = c.Count; i < cnt; i++) { logger.Trace(c[i].id + " " + c[i].value + " " + c[i].server); if (Double.IsNaN((c[i].value))) { //NonValueArray.Add(c[i].service, c[i].id); NonValueArray[c[i].service] = c[i].id; } } foreach (var pair in NonValueArray) { Console.WriteLine(pair.Key); } 
  • updated the answer, you need to connect the namespace - Bald
  • updated the answer, the error was in the absence of a closing parenthesis :) try now it should work - Bald
  • Tutorial C # in the hands and not overload sor with empty questions. - Mark Shevchenko
  • one
    @MarkShevchenko with a similar formulation, you can close any (or almost any) question - Bald
  • @ Bald56rus, no, not any. In this particular case, we are talking about ignorance of elementary things that are studied from textbooks. Such questions on StackOverflow - the right way to reduce the quality of the site. If you do not respond to them, after a couple of years, questions of students lazy to read a textbook will flourish here. - Mark Shevchenko

2 answers 2

 var grouped = array.GroupBy(x=>x.Value).ToList(); foreach(var g in grouped) { Console.WriteLine(string.Format("{0},{1}", g.Key, g.Count())); } 

writing without studio possible errors

to use linq you need to connect the appropriate namespace using System.Linq;

example here

  • can I comment? - des1roer
  • @ des1roer of course - Bald
  • The error "System.Array" does not contain a definition for "GroupBy" and the extension method "GroupBy" was not found, taking the type "System.Array" as the first argument (perhaps using the directive using or the assembly reference) - des1roer
  • InitParam.Param [] s = c.ToArray (); var grouped = s.GroupBy (x => x.service) .ToList (); foreach (var g in grouped) {Console.WriteLine (string.Format ("{0}, {1}"), g.Key, g.Count ()); } - des1roer
  • The index (counted from zero) must be greater than or equal to zero, but less than the size of the argument list. - des1roer

If you have a flat list of entities, then the resulting dictionary can be obtained as follows:

 var result = entities .Where(_ => Double.IsNaN(_.Value)) .GroupBy(_ => _.Name, _ => _.Id) .ToDictionary(_ => _.Key, _ => _.ToArray());