Solved

Help with LINQ order by query syntax

Posted on 2014-10-14
10
123 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
  • 4
  • 3
  • 2
10 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
Comment Utility
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 32

Assisted Solution

by:it_saige
it_saige earned 250 total points
Comment Utility
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
Comment Utility
I was so close.
Thank you both
0
 
LVL 32

Expert Comment

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

-saige-
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:John Bolter
Comment Utility
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 32

Expert Comment

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

-saige-
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
I have sent an attention request.

-saige-
0
 

Author Comment

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

Expert Comment

by:Fernando Soto
Comment Utility
Thank you -saige- and John for your efforts.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now