Solved

Help with LINQ order by query syntax

Posted on 2014-10-14
10
130 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 250 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 250 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
Industry Leaders: 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!

 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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