Click here to Skip to main content
11,719,185 members (76,890 online)
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 1:04am
amrutd407
Edited 11-Mar-12 0: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>
  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 Sergey Alexandrovich Kryukov 1,734
1 OriginalGriff 690
2 Maciej Los 569
3 Richard MacCutchan 540
4 F-ES Sitecore 510


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 28 Mar 2012
Copyright © CodeProject, 1999-2015
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