none
getting exception called atleast one object must implement IComparable RRS feed

  • Question

  •                

    var query = from incident in MainCache.Incident.AsEnumerable().Where(inc => (agencyIDs.Contains(inc.AgencyID) && inc.CallIsActive))

                               

    join sector in MainCache.Sector.AsEnumerable() on incident.CurrentSectorID equals sector.SectorID //into incidentGroup


                               

    join sectorPersonal in MainCache.SectorPersonnel.AsEnumerable().Where(sp => sp.Controlling == true)

                               

    on new { sector.SectorID } equals new { sectorPersonal.SectorID }

                               

    join personnel in MainCache.Personnel.AsEnumerable()

                               

    on sectorPersonal.PersonnelID equals personnel.PersonnelID

                               

    join machineInfo in MainCache.MachineInfo.AsEnumerable()

                               

    on sectorPersonal.MachineInfoID equals machineInfo.MachineInfoID into Data

                               

    from data in Data.DefaultIfEmpty()

                               

    group new { incident, sector, sectorPersonal, personnel, data } by new { UserName = personnel.Name, MachineName = data.Name, SectorName = sector.Name, incident.PriorityNumber } into Recall

                               

    let record = Recall.FirstOrDefault()

                               

    orderby new { record.personnel.Name, MachineName = record.data.Name, SectorName = record.sector.Name, record.incident.PriorityNumber }

                               

    select new


                                {

                                    UserName = record.personnel.Name,

                                    MachineName = record.data.Name,

                                    SectorName = record.sector.Name,

                                    PriorityNumber = record.incident.PriorityNumber,

                                    IncidentCount = Recall.Count()

                                };

                   

    foreach (var data in query)

    {

                        list.Add(

    new Tuple<string, string, string, int, int>(

                            data.UserName, data.MachineName, data.SectorName, data.PriorityNumber, data.IncidentCount));

                    }


    Monday, November 21, 2016 2:26 PM

Answers

  • This is a problem:

    let record = Recall.FirstOrDefault()
    orderby new { record.personnel.Name, MachineName = record.data.Name, SectorName = record.sector.Name, record.incident.PriorityNumber }

    The anonymous_object_creation_expression declares an anonymous type that implements neither IComparable nor IComparable<T>.

    Perhaps you want this instead:

    let record = Recall.FirstOrDefault()
    orderby record.personnel.Name, record.data.Name, record.sector.Name, record.incident.PriorityNumber

    Here's the whole thing with some test data.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApplication1
    {
        class Incident
        {
            internal string AgencyID;
            internal bool CallIsActive;
            internal string CurrentSectorID;
            internal int PriorityNumber;
        }
    
        class Sector
        {
            internal string SectorID;
            internal string Name;
        }
    
        class SectorPerson
        {
            internal string SectorID;
            internal string PersonnelID;
            internal string MachineInfoID;
            internal bool Controlling;
        }
    
        class Person
        {
            internal string PersonnelID;
            internal string Name;
        }
    
        class MachineInfo
        {
            internal string MachineInfoID;
            internal string Name;
        }
    
        static class MainCache
        {
            internal static List<Incident> Incident = new List<Incident>()
            {
                new Incident() { AgencyID = "B5", CallIsActive = true, CurrentSectorID = "JSL", PriorityNumber = 3 },
                new Incident() { AgencyID = "H0", CallIsActive = true, CurrentSectorID = "GDW", PriorityNumber = 1 },
            };
    
            internal static List<Sector> Sector = new List<Sector>()
            {
                new Sector() { SectorID = "JSL", Name = "Core" },
                new Sector() { SectorID = "GDW", Name = "Silo" },
            };
    
            internal static List<SectorPerson> SectorPersonnel = new List<SectorPerson>()
            {
                new SectorPerson() { SectorID = "JSL", PersonnelID = "4BCR", Controlling = true, MachineInfoID = "D9L" },
                new SectorPerson() { SectorID = "GDW", PersonnelID = "2UWU", Controlling = true, MachineInfoID = "P7P" },
            };
    
            internal static List<Person> Personnel = new List<Person>()
            {
                new Person() { PersonnelID = "4BCR", Name = "Biff" },
                new Person() { PersonnelID = "2UWU", Name = "Lucy" },
            };
    
            internal static List<MachineInfo> MachineInfo = new List<MachineInfo>()
            {
                new MachineInfo() { MachineInfoID = "D9L", Name = "Frob" },
                new MachineInfo() { MachineInfoID = "P7P", Name = "Gadget" },
            };
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                var agencyIDs = new List<string>() { "B5", "H0" };
                var list = new List<Tuple<string, string, string, int, int>>();
                var query = from incident in MainCache.Incident.AsEnumerable().Where(inc => (agencyIDs.Contains(inc.AgencyID) && inc.CallIsActive))
                            join sector in MainCache.Sector.AsEnumerable() on incident.CurrentSectorID equals sector.SectorID //into incidentGroup
                            join sectorPersonal in MainCache.SectorPersonnel.AsEnumerable().Where(sp => sp.Controlling == true)
                            on new { sector.SectorID } equals new { sectorPersonal.SectorID }
                            join personnel in MainCache.Personnel.AsEnumerable()
                            on sectorPersonal.PersonnelID equals personnel.PersonnelID
                            join machineInfo in MainCache.MachineInfo.AsEnumerable()
                            on sectorPersonal.MachineInfoID equals machineInfo.MachineInfoID into Data
                            from data in Data.DefaultIfEmpty()
                            group new { incident, sector, sectorPersonal, personnel, data }
                                by new { UserName = personnel.Name, MachineName = data.Name, SectorName = sector.Name, incident.PriorityNumber }
                                into Recall
                            let record = Recall.FirstOrDefault()
                            orderby record.personnel.Name, record.data.Name, record.sector.Name, record.incident.PriorityNumber
                            select new
                            {
                                UserName = record.personnel.Name,
                                MachineName = record.data.Name,
                                SectorName = record.sector.Name,
                                PriorityNumber = record.incident.PriorityNumber,
                                IncidentCount = Recall.Count()
                            };
    
                foreach (var data in query)
                {
                    list.Add(Tuple.Create(data.UserName, data.MachineName, data.SectorName, data.PriorityNumber, data.IncidentCount));
                }
            }
        }
    }
    • Proposed as answer by Kristin Xie Tuesday, November 22, 2016 2:18 AM
    • Marked as answer by BhaweshTanna Wednesday, November 23, 2016 5:17 AM
    Monday, November 21, 2016 5:55 PM