Home > Software design >  c# groupby / distinct a list of objects with a list inside
c# groupby / distinct a list of objects with a list inside

Time:02-25

having a class

public class Result
    {
        public string Department { get; set; }
        public List<string> ExtensionAttribute6 { get; set; }
    }

getting

foreach (var record in results2) {
  foreach (var item in record.ExtensionAttribute6) { 
    csv.WriteRecord(record);
    csv.WriteField(item);
    csv.NextRecord(); 
  } 
}

csv export

"test one", two
"test one", two
"test one", two
"test two", jump
"test two", jump

I would like to have it distinct output

"test one", two
"test two", jump

that's a how I get it

            var pageIterator = PageIterator<User>
             .CreatePageIterator(
             graphClient,
              pagedUsers,
              (user) =>
                {
                    messages.Add(user);
                    return true;
                }
         );

        await pageIterator.IterateAsync();

        log.LogInformation("alles abgerufen done");

        List<Result> results1 = new List<Result>();
        results1 = (
           from p in messages
           group p by p.Department into g
           select new Result()
           {
               Department = g.Key,
               ExtensionAttribute6 = g.Select(c => c.OnPremisesExtensionAttributes.ExtensionAttribute6).ToList()
           }
       ).ToList();
List<Result> results2 = new List<Result>();
            results2 = (
               from p in results1
               group p by (p.Department, p.ExtensionAttribute6) into g
               select new Result()
               {
                   Department = g.Key.Department,
                   ExtensionAttribute6 = g.Key.ExtensionAttribute6
               }
           ).Distinct().ToList();

i am sure i am doing it to long and to many steps, but its easier to debug and use at different states thanks for advice

I wold like to convert it to a new class

public class Result2
    {
        public string Department { get; set; }
        public string ExtensionAttribute6 { get; set; }
    }

with distinct output, sorry for confusing

CodePudding user response:

try this

var csv =
@"""test one"", two
""test one"", two
""test one"", two
""test two"", jump
""test two"", jump";

var csvArr = string.Join("\n", csv.Split("\n").Select(c => c.Trim()).Distinct());

result

"test one", two
"test two", jump

CodePudding user response:

Again not really clear what you ant but how about

foreach (var record in results2) {
    csv.WriteRecord(record);
    foreach (var item in record.ExtensionAttribute6) { 
      csv.WriteField(item);
    } 
    csv.NextRecord(); 
}

edit, seems like you have very odd data try this

foreach (var record in results2) {
    csv.WriteRecord(record);
    foreach (var item in record.ExtensionAttribute6.Distinct()) { 
      csv.WriteField(item);
    } 
    csv.NextRecord(); 
}

CodePudding user response:

I'm a little confused on what you're asking, but if you just want uniqe output for the .csv file, then you can use the .Distinct() extension method on your list:

        foreach (var record in results2.Distinct())
        {
                csv.WriteRecord(record); 
                csv.WriteField(record.ExtensionAttribute6);
                csv.NextRecord();

        }

Were you meaning to loop through every char in the record.ExtensionAttribute6 string? If ExtensionAttribute6 is actually a List<string> then you would want to move that .Distinct() down to to the inner foreach, like this:

        foreach (var record in results2)
        {
            foreach(var extensionAttribute in record.ExtensionsExtensionAttribute6.Distinct())
            {
                csv.WriteRecord(record);
                csv.WriteField(extensionAttribute);
                csv.NextRecord();
            }
        }
  • Related