![](/img/trans.png)
[英]Linq .Except function “At least one object must implement IComparable.”
[英]Error When Applying 'GroupBy' LINQ Clause: “At least one object must implement IComparable.”
我正在尝试按用户名然后按位置对自定义实体对象的集合进行分组。 对于特定用户,当我进入第二个“ groupby”时,总是会收到错误消息:
“至少一个对象必须实现IComparable。”
该用户的用户名为“#gill'n it”。 当我在该suername上执行第一个“ GroupBy”时,它返回Count = 6的匿名类型,这是正确的,但是该类型中的元素数为8,其中最后两个为null。 我认为最后两个null导致了我的问题,但是我不知道它们的来源。
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);
}
}
}
首先, null
值是红色鲱鱼。 .NET中集合的内部实现通常似乎具有两个元素的底数,可能会在集合操作中维持摊销时间 。 计数6是正确的。
其次,如果x.Positions[0]
的类型未实现IComparable
,并且找不到默认的比较器,则由于.OrderBy(x => x.Key)
会得到指定的异常,因为没有关于如何定义的定义对元素进行排序。 在这种情况下,请使用两个参数的扩展方法(如果计算this
参数,则为三个参数),该方法允许您指定要使用的IComparer
: http : //msdn.microsoft.com/zh-cn/library/bb549422( v = vs.110).aspx 。 或者,如果您拥有x.Positions[0]
类型的所有权,请对其进行修改以实现IComparable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.