Get Distinct departments : Distinct « LINQ « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » LINQ » DistinctScreenshots 
Get Distinct departments
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Tree<TreeNode> : IEnumerable<TreeNode> where TreeNode : IComparable<TreeNode> {
    public Tree(TreeNode nodeValue) {
        this.NodeData = nodeValue;
        this.LeftTree = null;
        this.RightTree = null;
    }

    public void Insert(TreeNode newItem) {
        TreeNode currentNodeValue = this.NodeData;
        if (currentNodeValue.CompareTo(newItem0) {
            if (this.LeftTree == null) {
                this.LeftTree = new Tree<TreeNode>(newItem);
            else {
                this.LeftTree.Insert(newItem);
            }
        else {
            if (this.RightTree == null) {
                this.RightTree = new Tree<TreeNode>(newItem);
            else {
                this.RightTree.Insert(newItem);
            }
        }
    }

    public void WalkTree() {
        if (this.LeftTree != null) {
            this.LeftTree.WalkTree();
        }

        Console.WriteLine(this.NodeData.ToString());

        if (this.RightTree != null) {
            this.RightTree.WalkTree();
        }
    }

    public TreeNode NodeData get; set; }
    public Tree<TreeNode> LeftTree get; set; }
    public Tree<TreeNode> RightTree get; set; }


    IEnumerator<TreeNode> IEnumerable<TreeNode>.GetEnumerator() {
        if (this.LeftTree != null) {
            foreach (TreeNode item in this.LeftTree) {
                yield return item;
            }
        }

        yield return this.NodeData;

        if (this.RightTree != null) {
            foreach (TreeNode item in this.RightTree) {
                yield return item;
            }
        }
    }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
        throw new NotImplementedException();
    }
}

class Employee : IComparable<Employee> {
    public string FirstName get; set; }
    public string LastName get; set; }
    public string Department get; set; }
    public int Id get; set; }

    public override string ToString() {
        return String.Format("Id: {0}, Name: {1} {2}, Dept: {3}"this.Id, this.FirstName, this.LastName, this.Department);
    }
    int IComparable<Employee>.CompareTo(Employee other) {
        if (other == null)
            return 1;

        if (this.Id > other.Id)
            return 1;

        if (this.Id < other.Id)
            return -1;

        return 0;
    }
}

class Program {
    static void Main() {
        Tree<Employee> empTree = new Tree<Employee>(new Employee Id = 1, FirstName = "Janet", LastName = "Gates", Department = "IT" });
        empTree.Insert(new Employee Id = 2, FirstName = "O", LastName = "G", Department = "Marketing" });
        empTree.Insert(new Employee Id = 4, FirstName = "K", LastName = "H", Department = "IT" });
        empTree.Insert(new Employee Id = 6, FirstName = "L", LastName = "H", Department = "Sales" });
        empTree.Insert(new Employee Id = 3, FirstName = "E", LastName = "L", Department = "Sales" });
        empTree.Insert(new Employee Id = 5, FirstName = "D", LastName = "L", Department = "Marketing" });

        var depts = (from d in empTree select d.Department).Distinct();

        foreach (var dept in depts)
            Console.WriteLine("Department: {0}", dept);
    }
}

 
Related examples in the same category
1.Use Linq Distinct to get the distinct value in an array
2.Set Operators: Distinct
3.prints the unique Category names
4.Distinct Operator
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.