问题描述:

I've a issue and unable to resolved, have a look at my code.

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

});

Scenario is that I want to get Top 3 Customers with highest sales. But it gives me the error as "Atleast one object must implement IComparable". Surprisingly, I've tried to order by customer name and it worked fine, but its not fine with order by sales.

网友答案:

As @leppie mentioned you order by a List and your program don't know how to compare this 2 lists.

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

I see that you have only 1 element in your list. Based on this you can order by .First() (and only) element from list.

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.First()).Take(3).ToList(),
    ChartConfig = config
});
网友答案:

My doubt is why we need a list to hold a single value, should n't

<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>

if I am correct , then the changing this line will correct the error.

相关阅读:
Top