Link to home
Start Free TrialLog in
Avatar of John Bolter
John Bolter

asked on

Help with LINQ order by query syntax

Hi, the following code works fine

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication198
{
    class Test
    {
        public String Name { get; set; }
        public int ID { get; set; }
    }

    class Program
    {
        public void Start()
        {
            Test t1 = new Test{Name = "Fred", ID = 123 };
            Test t2 = new Test { Name = "John", ID = 4 };
            List<Test> t = new List<Test> { t1, t2 };

            var p = (new String[] { "All Employees" }).Union(from x
                                                                in t
                                                                orderby x.Name ascending
                                                                  select x.Name                                                                 
                                                            ).ToList<String>();
            //p is a List<String> that I can assign to a dropdownList.DataSource
            //and "All Employees" appears at the top, most of the time
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            p.Start();
        }
    }
}

Open in new window


It works.
"p" is used as a DataSource for a DropDownList
All of the time in this test program things always work.
Most but not all the time it works in a more complex program. The problem I am seeing is that "All Employees" in my real application only sometimes appears at the top of the list. I always need it at the top of the list in my drop down.

Can someone help how I can "order by" to ensure that "p" is always a list where p[0]="All Employees".

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John Bolter
John Bolter

ASKER

I was so close.
Thank you both
@John, while I appreciate the points.  I really think that Fernando deserves credit also.

-saige-
I tried to split it 50:50 but I couldn't figure out how. And now I can't seem to undo it. I thought it too.
We can have an administrator reopen so that you can re-assign the points accordingly.

-saige-
I have sent an attention request.

-saige-
Thanks & sorry about this guys, 50/50, it was my error.
I can't see to write LINQ or use this website :-(
Thank you -saige- and John for your efforts.