Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have problem while grouping in listbox. Actually my table structure is as follows.
 
Emp_ID EmpName EmpParent_ID
 
1000002 Vivek 1000005
1000003 Rohit 1000005
1000005 Ajay 1000031
1000006 Sanjay 1000005
1000011 Hemant -1
1000022 Amit 1000005
1000024 Aakash 1000006
1000027 Ameya 1000005
1000030 Ram 1000005
1000042 Arvind -1
 
Now I want to group on EmpParent_ID. Output should be like this:
 
Ajay
Vivek
Rohit
Sanjay
Amit
Ameya
Ram
Sanjay
Aakash
 
If this is not possible using ListBox then please suggest me other way to work it properly...
Posted 4-Mar-12 2:04am
amrutd384
Edited 11-Mar-12 1:05am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It is very simple, are you using Entity Framework or Storage Procedure or simple select ?
 
either way you can filter on database
 
on your select just put something like that
 
select distinct(EmpName),Emp_ID, EmpParent_ID from TABLENAME group by EmpName
 
or you can use LINQ
 
example
 
    class Program
    {
        static void Main(string[] args)
        {
            List<person> pessoas = new List<person>()
            {
                new Person(){ id = 1, name = "fernando"},
                new Person(){ id = 2, name = "rodrigo"},
                new Person(){ id = 4, name = "fernando"},
                new Person(){ id = 5, name = "rogerio"},
                new Person(){ id = 6, name = "fernando"},
                new Person(){ id = 7, name = "davi"}
            };
 
            foreach (Person pes in pessoas.Distinct(new CustomComparer()))
            {
                Console.WriteLine(pes.name);
            }
 
            Console.Read();
        }
    }
 
    public class Person 
    {
        public int id { get; set; }
        public string name { get; set; }
    }
 
    public class CustomComparer : IEqualityComparer<person>
    {
        public bool Equals(Person x, Person y)
        {
            return x.name.Equals(y.name);
        }
 
        public int GetHashCode(Person obj)
        {
            return this.GetHashCode();
        }
    }</person></person></person>
  Permalink  
Comments
fealbernaz at 28-Mar-12 13:16pm
   
ops, remove group by EmpName from the select

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 319
1 Maciej Los 295
2 Sergey Alexandrovich Kryukov 230
3 CPallini 205
4 Sinisa Hajnal 166
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 28 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100