Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 至少一个对象必须实现IComparable错误_C#_Linq - Fatal编程技术网

C# 至少一个对象必须实现IComparable错误

C# 至少一个对象必须实现IComparable错误,c#,linq,C#,Linq,我有一个问题,我无法解决它。看看我的代码 CustomerProductivity.Add(new ChartDataViewModel { Series = new List<string> { "Sales" }, ChartType = "bar", Data = SalesStatistics .GroupBy(b => b.CustomerName) .Select(g => new ChartDataEl

我有一个问题,我无法解决它。看看我的代码

CustomerProductivity.Add(new ChartDataViewModel
{
    Series = new List<string> { "Sales" },
    ChartType = "bar",
    Data = SalesStatistics
        .GroupBy(b => b.CustomerName)
        .Select(g => new ChartDataElement()
        {
            X = g.Key,
            Y = new List<int>() { (int)g.Sum(b => b.Sales) }
        }).OrderByDescending(f=> f.Y).Take(3).ToList(),
    ChartConfig = config
});
<pre>
   Y = new List<int>() { (int)g.Sum(b => b.Sales) }
</pre>
should be 
<pre>
  Y =  (int)g.Sum(b => b.Sales) \\\\ as the sum will a int. 
</pre>
CustomerProductivity.Add(新的ChartDataViewModel)
{
系列=新列表{“销售”},
ChartType=“bar”,
数据=销售统计数据
.GroupBy(b=>b.CustomerName)
.Select(g=>newchartDataElement()
{
X=g.键,
Y=新列表(){(int)g.Sum(b=>b.Sales)}
}).OrderByDescending(f=>f.Y).Take(3).ToList(),
ChartConfig=config
});

情况是,我希望获得销售额最高的前三名客户。但它给了我一个错误,因为“至少有一个对象必须实现IComparable”。令人惊讶的是,我尝试按客户名称订购,效果很好,但如果我按销售订购,效果就不好。

正如@leppie提到的,您是按
列表订购的,您的程序不知道如何比较这两个列表

List<int> > List<int> // How i compare this? by Count? by elements? what property of elements? and so one

我的疑问是为什么我们需要一个列表来保存一个值,不是吗


Y=新列表(){(int)g.Sum(b=>b.Sales)}
应该是
Y=(int)g.Sum(b=>b.Sales)\\\\\因为总和将是一个int。

如果我是正确的,那么更改此行将更正错误

你怎么能按列表排序?我想你想要的是降序(f=>f.Y.First()).Take(3).ToList(),
你为什么要把聚合结果放在列表中?@Luaan他创建了一个
ChartDataElement
这是
Y
类型,我想他没有选择。@adricadar啊,说得对。对LINQ来说不太实用…:D
<pre>
   Y = new List<int>() { (int)g.Sum(b => b.Sales) }
</pre>
should be 
<pre>
  Y =  (int)g.Sum(b => b.Sales) \\\\ as the sum will a int. 
</pre>