简体   繁体   中英

Error When Applying 'GroupBy' LINQ Clause: “At least one object must implement IComparable.”

I'm trying to group a collection of custom entity object by username, then by position. For a specific user, when I get to the second 'groupby', I always get the error:

"At least one object must implement IComparable."

The user in question has a username "#gill'n it". When I execute the first "GroupBy" on that suername, it returns an anonymous type of Count=6, which is correct, but the number of elements in the type is 8 where the final two are null. I think those final two nulls are causing my problem, but I can't figure out where they're coming from.

     List<CheatSheet> allRelevantCheatSheets = CheatSheet.GetCheatSheets(Globals.FOOString)   // only grade football sheets
                                              .Where(x => x.Username != String.Empty)      // only grade user sheets
                                              .Where(x => x.SeasonCode == currrentFOOSeason)  // only grade sheet for the current season
                                              .Where(x => x.LastUpdated < kickoffDate)         // only grade sheets before the kickoff date
                                              .Where(x => x.Positions.Count == 1)    // only grade single-position sheets
                                              .Where(x => (bool)x.MappedProperties[CSProperty.PPRLeague.ToString()] == false) // only grade standard socring sheets
                                              .ToList();

  foreach (var userSheetGroup in allRelevantCheatSheets.GroupBy(x => x.Username).OrderBy(x => x.Key))
  {

    int newCheatSheetID = 0;
    groupCounter++;

    // then group userSheets by position
    foreach (var targetUserPositionalSheetGroup in userSheetGroup.GroupBy(x => x.Positions[0]).OrderBy(x => x.Key))
    {
      // finally limit the type of sheet returned to only 1 (the latest one ordered by date), must cast to list in order to avoid casting error, then take first item
      CheatSheet userTopPositionSheet = (CheatSheet)targetUserPositionalSheetGroup.OrderBy(x => x.CheatSheetID).OrderBy(x => x.Positions[0]).OrderByDescending(x => x.LastUpdated).Take(1).ToList()[0];
      newCheatSheetID = ArchiveCheatSheet(userTopPositionSheet);
      if (newCheatSheetID == 0)
      {
        errorCounter++;
      }
      else
      {
        sheetCounter++;

        ArchiveCheatSheetItems(userTopPositionSheet.CheatSheetID, newCheatSheetID);
      }
    }
  }

GroupBy错误

First, the null values are a red herring. Internal implementations of collections in .NET often seem to have underlying element counts that are powers of two, likely to maintain amortized time in collection manipulation operations. The count of 6 is correct.

Second, if the type of x.Positions[0] does not implement IComparable , and no default comparer can be found, you would get the specified exception due to .OrderBy(x => x.Key) because there is no definition of how to sort the elements. If this is the case, use the two-argument extension method (three-argument if you count this ) that allows you to specify the IComparer to use: http://msdn.microsoft.com/en-us/library/bb549422(v=vs.110).aspx . Or, if you have ownership of the type of x.Positions[0] , modify it to implement IComparable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM