Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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
Posted
Updated 23-Nov-18 9:04am

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[^]
 
Share this answer
 
v2
I got it!

C#
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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900