至少有一个对象必须实现icomparable。 LINQ C# [英] At least one object must implement icomparable. LINQ C#

查看:1807
本文介绍了至少有一个对象必须实现icomparable。 LINQ C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我有一个List,我正在申请GroupBy,Orderby&然后使用LINQ-C#。



当我更改requestDate和备注时我得到以下错误。



Dear All,

I have a List and i'm appling GroupBy, Orderby & ThenBy using LINQ-C#.

When i change the requestDate and remarks i'm getting the following erro.

At least one object must implement IComparable.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: At least one object must implement IComparable.





什么我试过了:





What I have tried:

IEnumerable<CandidateRefundVoucherInfo> requestVouchers = CandidateRefundVoucherController.Instance.GetByCenterID(16);

            if (requestVouchers.Count() > 0)
            {
                var CandidateRefundVouchers = requestVouchers.
                    Select(items => new
                    {
                        CertificateID = items.Certificate.ID,
                        items.Certificate.Code,
                        items.Certificate.NameEN,
                        items.Certificate.NameAR,
                        items.Remarks,
                        items.RequestDate,
                        items.NumberOfRefundVoucherRequest,
                        items.StatusType,
                        UpdatedByUser = items.UpdatedByUser.NameEN
                    }).
                    GroupBy(i => new
                    {
                        i.CertificateID,
                        i.Code,
                        i.NameEN,
                        i.NameAR,
                        i.Remarks,
                        i.RequestDate,
                        i.NumberOfRefundVoucherRequest,
                        i.StatusType,
                        i.UpdatedByUser
                    })
                        .OrderByDescending(d => new { d.Key.RequestDate })
                        .ThenBy(l => new { l.Key.Remarks })
                        .Select(item => new
                        {
                            ID = item.Key.CertificateID,
                            Code = item.Key.Code,
                            NameEN = item.Key.NameEN,
                            NameAR = item.Key.NameAR,
                            RequestedVouchers = item.Sum(items => items.NumberOfRefundVoucherRequest),
                            Status = item.Key.StatusType,
                            RequestedDate = item.Key.RequestDate.ToString("dddd, dd MMMM yyyy"),
                            Remarks = item.Key.Remarks,
                            UpdatedBy = item.Key.UpdatedByUser,
                            BindLink = BindCode(item.Key.CertificateID, item.Key.Code)
                        });

                rptCandidateRefundVouchers.DataSource = CandidateRefundVouchers;
                rptCandidateRefundVouchers.DataBind();







任何人都可以帮助我。





谢谢




Can anyone please help me.


Thanks

推荐答案

错误信息非常清楚,订购一套事情,必须有一种比较两件事的方法。您需要实现IComparable接口,以提供与另一个实例的比较。如果您的代码出现问题,将发生在OrderByDescending方法。



请参阅以下链接:

c# - 至少有一个对象必须实现IComparable调用OrderBy() - Stack Overflow [ ^ ]
The error message is very clear, to order a set of things, there must be a way to compare two things. You need to implement IComparable interface, to provide compare versus another instance. In the case of your code problem, will occurs at OrderByDescending method.

See the below link as well:
c# - At least one object must implement IComparable calling OrderBy() - Stack Overflow[^]


我知道了!



I got it!

IEnumerable<CandidateRefundVoucherInfo> candidateRefundVoucherList = requestVouchers;
                var refundVouchers = candidateRefundVoucherList.OrderByDescending(d => d.RequestDate).
                    GroupBy(r => r.RequestDate).Select(item => new {
                    ID = item.First().Certificate.ID,
                    Code = item.First().Certificate.Code,
                    NameEN = item.First().Certificate.NameEN,
                    NameAR = item.First().Certificate.NameAR,
                    RequestedVouchers = item.Sum(items => items.NumberOfRefundVoucherRequest),
                    Status = item.First().StatusType,
                    RequestedDate = item.First().RequestDate.ToString("dddd, dd MMMM yyyy"),
                    Remarks = item.First().Remarks,
                    UpdatedBy = item.First().UpdatedByUser.NameEN,
                    BindLink = BindCode(item.First().Certificate.ID, item.First().Certificate.Code, item.First().RequestDate.ToShortDateString())
                });

                rptCandidateRefundVouchers.DataSource = refundVouchers;
                rptCandidateRefundVouchers.DataBind();


这篇关于至少有一个对象必须实现icomparable。 LINQ C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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