views:

336

answers:

1

I have the below LINQ method which gets called from a User Control and dumped straight to a standard DataGridView. My users want it default sorted, first, by DisenrollmentDate and then, Member Name. Right now, with the code below, it sorts by DisenrollmentDate ONLY.


BLLCmo.cs

    public static DataTable GetAllMembers(Guid workerID)
    {
        var AllEnrollees = from enrollment in context.tblCMOEnrollments
                               where enrollment.CMOSocialWorkerID == workerID || enrollment.CMONurseID == workerID
                               join supportWorker in context.tblSupportWorkers on enrollment.EconomicSupportWorkerID equals supportWorker.SupportWorkerID into workerGroup
                               from worker in workerGroup.DefaultIfEmpty()
                               orderby enrollment.DisenrollmentDate ascending 
                               select new
                                       {
                                           enrollment.ClientID,
                                           MemberName = BLLConnect.MemberName(enrollment.ClientID),
                                           NurseName = BLLAspnetdb.NurseName(enrollment.CMONurseID),
                                           SocialWorkerName =BLLAspnetdb.SocialWorkerName(enrollment.CMOSocialWorkerID),
                                           enrollment.DisenrollmentDate,
                                           enrollment.EnrollmentDate,
                                           ESFirstName = worker.FirstName,
                                           ESLastName = worker.LastName,
                                           ESPhone = worker.Phone
                                       };

        var dataTable = AllEnrollees.CopyLinqToDataTable();

        return dataTable;
    }


BLLConnect.cs

    public static String MemberName(Int32 personID)
    {
        var memberName = from person in context.tblPersons
                         where person.PersonID == personID
                         orderby person.LastName ascending 
                         select person.FirstName + " " + person.LastName;

        return memberName.SingleOrDefault();
    }
+2  A: 

You can add more ordering clauses in your orderby statement, separated by a comma.

    public static DataTable GetAllMembers(Guid workerID)
    {
        var AllEnrollees = from enrollment in context.tblCMOEnrollments
                               where enrollment.CMOSocialWorkerID == workerID || enrollment.CMONurseID == workerID
                               join supportWorker in context.tblSupportWorkers on enrollment.EconomicSupportWorkerID equals supportWorker.SupportWorkerID into workerGroup
                               from worker in workerGroup.DefaultIfEmpty()
                               let memberName = BLLConnect.MemberName(enrollment.ClientID)
                               orderby enrollment.DisenrollmentDate ascending, memberName ascending
                               select new
                                       {
                                           enrollment.ClientID,
                                           MemberName = memberName,
                                           NurseName = BLLAspnetdb.NurseName(enrollment.CMONurseID),
                                           SocialWorkerName =BLLAspnetdb.SocialWorkerName(enrollment.CMOSocialWorkerID),
                                           enrollment.DisenrollmentDate,
                                           enrollment.EnrollmentDate,
                                           ESFirstName = worker.FirstName,
                                           ESLastName = worker.LastName,
                                           ESPhone = worker.Phone
                                       };

        var dataTable = AllEnrollees.CopyLinqToDataTable();

        return dataTable;
    }

Note that I used the let keyword to extract the memberName once and then use it both for ordering and the return value.

Mark Seemann
This seems like it will work. Thanks!!! Unfortunately it is throwing an exception on the `CopyLinqToDataTable` part for me...I should just quite using `DataTables` :\
Refracted Paladin
No matter what I do, when I go to assign the results to the DataGridView's DataSource it throws this exception--> `Method 'System.String MemberName(Int32)' has no supported translation to SQL.`
Refracted Paladin
Yes, but that's a different issue. Basically, it tells you that it can't translate the Expression Tree that contains a method call into T-SQL. Try to inline that expression into the query.
Mark Seemann
Yeah, I kinda figured it was seperate. Thanks for the solution to the first problem. Now off to research in lining expressions in to queries
Refracted Paladin