在Linq分组,排名和前10名 [英] Group by, Order by and top 10 in Linq

查看:740
本文介绍了在Linq分组,排名和前10名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从列表中获得前十大保险金额请帮助。我试过这种方式,但它没有用。



公共类Top10Insurance 
{
public string Amt {get ;组; }
}

列表< Top10Insurance> result = InfoDB
.GroupBy(a => a.InsuranceName)
。选择(b => new Top10Insurance
{
Amt = b.Sum(c => c.BalanceAmount).ToString(),
})。OrderByDescending(d => d.Amt).Take(10)。ToList();

解决方案

OP回答!



Mohammed_Faisal [ ^ ]在评论中写道:

当我转换时:

< pre lang =c#> public class Top10Insurance
{
public double Amt { get ; set ; }
}

Amt = b.Sum(c = > c.BalanceAmount)



i出现此错误:

< br /> 
无法将类型'decimal?'隐式转换为'double'。存在显式转换(你是否错过演员?)





他回答了自己的问题:

 Amt =( double )b.Sum(c = >  c.BalanceAmount)


How do i get the top 10 Insurance Amount from the List please help. I have tried in this way but its not working.

 public class Top10Insurance
    {        
        public string Amt { get; set; }        
    }

List<Top10Insurance> result = InfoDB
                 .GroupBy(a => a.InsuranceName)
                 .Select(b => new Top10Insurance
                 {
                    Amt = b.Sum(c => c.BalanceAmount).ToString(),
                 }).OrderByDescending(d => d.Amt).Take(10).ToList();

解决方案

Answered by OP!

Mohammed_Faisal[^] wrote in the comment to the question:

When i converted :

public class Top10Insurance
{
public double Amt { get; set; }
}

Amt = b.Sum(c => c.BalanceAmount)


i got this error:

<br />
Cannot implicitly convert type 'decimal?' to 'double'. An explicit conversion exists (are you missing a cast?)



And he answered his own question:

Amt = (double)b.Sum(c => c.BalanceAmount)


这篇关于在Linq分组,排名和前10名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆