?
Solved

Help with LINQ order by query syntax

Posted on 2014-10-14
10
Medium Priority
?
133 Views
Last Modified: 2016-02-15
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
0
Comment
Question by:John Bolter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
10 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1000 total points
ID: 40380116
Hi John;

I have removed the method call to Union and placed an Insert method call to insert  "All Employees" at the beginning of the List.

var p = (from x in t
         orderby x.Name ascending
         select x.Name
         ).ToList<String>();

// Insert this value at the begining of the list.
p.Insert(0, "All Employees");

Open in new window

0
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 1000 total points
ID: 40380273
I agree with Fernando.

You either insert the "All Employees" item to the list at the 0 position:
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 };
			List<String> p = new List<String>();

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

			p.Insert(0, "All Employees");

			foreach (var employee in p)
				Console.WriteLine(employee);
			Console.ReadLine();
		}

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

Open in new window

Output:Capture.JPGOr create the list with the first item specified as "All Employees" and then use List.AddRange(IEnumerable<String>) to add your remaining elements:
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 };
			List<String> p = new List<String>() { "All Employees" };
			p.AddRange(from x in t
					 orderby x.Name ascending
					 select x.Name);
			//p is a List<String> that I can assign to a dropdownList.DataSource
			//and "All Employees" appears at the top, most of the time

			foreach (var employee in p)
				Console.WriteLine(employee);
			Console.ReadLine();
		}

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

Open in new window

Output:Capture.JPG
-saige-
0
 

Author Comment

by:John Bolter
ID: 40380285
I was so close.
Thank you both
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 34

Expert Comment

by:it_saige
ID: 40380291
@John, while I appreciate the points.  I really think that Fernando deserves credit also.

-saige-
0
 

Author Comment

by:John Bolter
ID: 40380297
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.
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40380302
We can have an administrator reopen so that you can re-assign the points accordingly.

-saige-
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40380304
I have sent an attention request.

-saige-
0
 

Author Comment

by:John Bolter
ID: 40380318
Thanks & sorry about this guys, 50/50, it was my error.
I can't see to write LINQ or use this website :-(
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40380553
Thank you -saige- and John for your efforts.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question