0

I have a list of type which contains 5 fields. From this list I wish to retrieve values where it groups IssueType & ID together. So that in the table below the issueType ABC & Id = 2 only appears once in my new list. How do I do this?

I should also point out that IssueType is an enumeration. Not sure if this makes any difference

IssueType    Id
ABC          1
ABC          2
ABC          2
ABC          3
XZY          4
XYZ          1

I have tried using the groupby in LINQ.

List<IssueLog> uniqueErrorLog = ErrorLog.GroupBy(err => err.IssueType, err => err.Id).Select(err => err).ToList();

Update

Ok so I have since tried the line below.

 var uniqueErrorLog = ErrorLog.GroupBy(iss => new { iss.IssueType, iss.Id });

I then check the output and the grouping seems to have made no difference. Going back to my table example I can still see two ABC's with ID = 2 being output when I only want one.

 foreach (var issueType in uniqueErrorLog)
            {
                foreach(var issue in issueType)
                    Console.WriteLine(issue.IssueType + "," + issue.Id);                    
            }
mHelpMe
  • 6,336
  • 24
  • 75
  • 150
  • http://stackoverflow.com/questions/10960953/splitting-linq-list-by-grouping – Jalpesh Vadgama Mar 06 '14 at 09:51
  • @Chris please see edited post for what I have tried so far. – mHelpMe Mar 06 '14 at 09:54
  • 1
    Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Mar 06 '14 at 10:03
  • Do you actually want them grouped or do you want to remove duplicates? Grouping won't remove duplicates, it will just allow you to access them by groups. Each group will have an enumerable of the grouped elements (ie the ones that match the grouping criteria). You probably just want `.Distinct()` in this case from the sounds of things. Or when grouping just use the key of the group rather than enumerating through the contents of each group. – Chris Mar 06 '14 at 14:22
  • I do not want to remove the duplicates. So can I use Distinct on multiple fields? – mHelpMe Mar 06 '14 at 15:43

1 Answers1

-1
myList.GroupBy(e => string.Format("{0}_{1}", e.IssueType, e.Id))
Mau
  • 14,234
  • 2
  • 31
  • 52