Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 11-Mar-12 0:05am
v2

1 solution

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

C#
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>
 
Share this answer
 
Comments
fealbernaz 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900