<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FLINQ%2FQuery</id>
		<title>Csharp/C Sharp/LINQ/Query - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FLINQ%2FQuery"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/LINQ/Query&amp;action=history"/>
		<updated>2026-04-30T01:26:23Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp/LINQ/Query&amp;diff=522&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/LINQ/Query&amp;diff=522&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:18Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp/LINQ/Query&amp;diff=523&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/LINQ/Query&amp;diff=523&amp;oldid=prev"/>
				<updated>2010-05-26T11:38:48Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==All employees in a tree are grouped by department==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class Tree&amp;lt;TreeNode&amp;gt; : IEnumerable&amp;lt;TreeNode&amp;gt; where TreeNode : IComparable&amp;lt;TreeNode&amp;gt; {&lt;br /&gt;
    public Tree(TreeNode nodeValue) {&lt;br /&gt;
        this.NodeData = nodeValue;&lt;br /&gt;
        this.LeftTree = null;&lt;br /&gt;
        this.RightTree = null;&lt;br /&gt;
    }&lt;br /&gt;
    public void Insert(TreeNode newItem) {&lt;br /&gt;
        TreeNode currentNodeValue = this.NodeData;&lt;br /&gt;
        if (currentNodeValue.rupareTo(newItem) &amp;gt; 0) {&lt;br /&gt;
            if (this.LeftTree == null) {&lt;br /&gt;
                this.LeftTree = new Tree&amp;lt;TreeNode&amp;gt;(newItem);&lt;br /&gt;
            } else {&lt;br /&gt;
                this.LeftTree.Insert(newItem);&lt;br /&gt;
            }&lt;br /&gt;
        } else {&lt;br /&gt;
            if (this.RightTree == null) {&lt;br /&gt;
                this.RightTree = new Tree&amp;lt;TreeNode&amp;gt;(newItem);&lt;br /&gt;
            } else {&lt;br /&gt;
                this.RightTree.Insert(newItem);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public void WalkTree() {&lt;br /&gt;
        if (this.LeftTree != null) {&lt;br /&gt;
            this.LeftTree.WalkTree();&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(this.NodeData.ToString());&lt;br /&gt;
        if (this.RightTree != null) {&lt;br /&gt;
            this.RightTree.WalkTree();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public TreeNode NodeData { get; set; }&lt;br /&gt;
    public Tree&amp;lt;TreeNode&amp;gt; LeftTree { get; set; }&lt;br /&gt;
    public Tree&amp;lt;TreeNode&amp;gt; RightTree { get; set; }&lt;br /&gt;
&lt;br /&gt;
    IEnumerator&amp;lt;TreeNode&amp;gt; IEnumerable&amp;lt;TreeNode&amp;gt;.GetEnumerator() {&lt;br /&gt;
        if (this.LeftTree != null) {&lt;br /&gt;
            foreach (TreeNode item in this.LeftTree) {&lt;br /&gt;
                yield return item;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        yield return this.NodeData;&lt;br /&gt;
        if (this.RightTree != null) {&lt;br /&gt;
            foreach (TreeNode item in this.RightTree) {&lt;br /&gt;
                yield return item;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {&lt;br /&gt;
        throw new NotImplementedException();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Employee : IComparable&amp;lt;Employee&amp;gt; {&lt;br /&gt;
    public string FirstName { get; set; }&lt;br /&gt;
    public string LastName { get; set; }&lt;br /&gt;
    public string Department { get; set; }&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public override string ToString() {&lt;br /&gt;
        return String.Format(&amp;quot;Id: {0}, Name: {1} {2}, Dept: {3}&amp;quot;, this.Id, this.FirstName, this.LastName, this.Department);&lt;br /&gt;
    }&lt;br /&gt;
    int IComparable&amp;lt;Employee&amp;gt;.rupareTo(Employee other) {&lt;br /&gt;
        if (other == null)&lt;br /&gt;
            return 1;&lt;br /&gt;
        if (this.Id &amp;gt; other.Id)&lt;br /&gt;
            return 1;&lt;br /&gt;
        if (this.Id &amp;lt; other.Id)&lt;br /&gt;
            return -1;&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Program {&lt;br /&gt;
    static void Main() {&lt;br /&gt;
        Tree&amp;lt;Employee&amp;gt; empTree = new Tree&amp;lt;Employee&amp;gt;(new Employee { Id = 1, FirstName = &amp;quot;Janet&amp;quot;, LastName = &amp;quot;Gates&amp;quot;, Department = &amp;quot;IT&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 2, FirstName = &amp;quot;O&amp;quot;, LastName = &amp;quot;G&amp;quot;, Department = &amp;quot;Marketing&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 4, FirstName = &amp;quot;K&amp;quot;, LastName = &amp;quot;H&amp;quot;, Department = &amp;quot;IT&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 6, FirstName = &amp;quot;L&amp;quot;, LastName = &amp;quot;H&amp;quot;, Department = &amp;quot;Sales&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 3, FirstName = &amp;quot;E&amp;quot;, LastName = &amp;quot;L&amp;quot;, Department = &amp;quot;Sales&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 5, FirstName = &amp;quot;D&amp;quot;, LastName = &amp;quot;L&amp;quot;, Department = &amp;quot;Marketing&amp;quot; });&lt;br /&gt;
        var employeesByDept = empTree.GroupBy(e =&amp;gt; e.Department);&lt;br /&gt;
        foreach (var dept in employeesByDept)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Department: {0}&amp;quot;, dept.Key);&lt;br /&gt;
            foreach (var emp in dept)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;\t{0} {1}&amp;quot;, emp.FirstName, emp.LastName);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Chaining Query Operators/extracts all strings containing the letter &amp;quot;a&amp;quot;, sorts them by length, and then converts the results to uppercase==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
class LinqDemo {&lt;br /&gt;
    static void Main() {&lt;br /&gt;
        string[] names = { &amp;quot;J&amp;quot;, &amp;quot;P&amp;quot;, &amp;quot;G&amp;quot;, &amp;quot;P&amp;quot; };&lt;br /&gt;
        IEnumerable&amp;lt;string&amp;gt; query = names&lt;br /&gt;
          .Where(n =&amp;gt; n.Contains(&amp;quot;a&amp;quot;))&lt;br /&gt;
          .OrderBy(n =&amp;gt; n.Length)&lt;br /&gt;
          .Select(n =&amp;gt; n.ToUpper());&lt;br /&gt;
        foreach (string name in query) Console.Write(name + &amp;quot;|&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Get Distinct departments with condition==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class Tree&amp;lt;TreeNode&amp;gt; : IEnumerable&amp;lt;TreeNode&amp;gt; where TreeNode : IComparable&amp;lt;TreeNode&amp;gt; {&lt;br /&gt;
    public Tree(TreeNode nodeValue) {&lt;br /&gt;
        this.NodeData = nodeValue;&lt;br /&gt;
        this.LeftTree = null;&lt;br /&gt;
        this.RightTree = null;&lt;br /&gt;
    }&lt;br /&gt;
    public void Insert(TreeNode newItem) {&lt;br /&gt;
        TreeNode currentNodeValue = this.NodeData;&lt;br /&gt;
        if (currentNodeValue.rupareTo(newItem) &amp;gt; 0) {&lt;br /&gt;
            if (this.LeftTree == null) {&lt;br /&gt;
                this.LeftTree = new Tree&amp;lt;TreeNode&amp;gt;(newItem);&lt;br /&gt;
            } else {&lt;br /&gt;
                this.LeftTree.Insert(newItem);&lt;br /&gt;
            }&lt;br /&gt;
        } else {&lt;br /&gt;
            if (this.RightTree == null) {&lt;br /&gt;
                this.RightTree = new Tree&amp;lt;TreeNode&amp;gt;(newItem);&lt;br /&gt;
            } else {&lt;br /&gt;
                this.RightTree.Insert(newItem);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public void WalkTree() {&lt;br /&gt;
        if (this.LeftTree != null) {&lt;br /&gt;
            this.LeftTree.WalkTree();&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(this.NodeData.ToString());&lt;br /&gt;
        if (this.RightTree != null) {&lt;br /&gt;
            this.RightTree.WalkTree();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public TreeNode NodeData { get; set; }&lt;br /&gt;
    public Tree&amp;lt;TreeNode&amp;gt; LeftTree { get; set; }&lt;br /&gt;
    public Tree&amp;lt;TreeNode&amp;gt; RightTree { get; set; }&lt;br /&gt;
&lt;br /&gt;
    IEnumerator&amp;lt;TreeNode&amp;gt; IEnumerable&amp;lt;TreeNode&amp;gt;.GetEnumerator() {&lt;br /&gt;
        if (this.LeftTree != null) {&lt;br /&gt;
            foreach (TreeNode item in this.LeftTree) {&lt;br /&gt;
                yield return item;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        yield return this.NodeData;&lt;br /&gt;
        if (this.RightTree != null) {&lt;br /&gt;
            foreach (TreeNode item in this.RightTree) {&lt;br /&gt;
                yield return item;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {&lt;br /&gt;
        throw new NotImplementedException();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Employee : IComparable&amp;lt;Employee&amp;gt; {&lt;br /&gt;
    public string FirstName { get; set; }&lt;br /&gt;
    public string LastName { get; set; }&lt;br /&gt;
    public string Department { get; set; }&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public override string ToString() {&lt;br /&gt;
        return String.Format(&amp;quot;Id: {0}, Name: {1} {2}, Dept: {3}&amp;quot;, this.Id, this.FirstName, this.LastName, this.Department);&lt;br /&gt;
    }&lt;br /&gt;
    int IComparable&amp;lt;Employee&amp;gt;.rupareTo(Employee other) {&lt;br /&gt;
        if (other == null)&lt;br /&gt;
            return 1;&lt;br /&gt;
        if (this.Id &amp;gt; other.Id)&lt;br /&gt;
            return 1;&lt;br /&gt;
        if (this.Id &amp;lt; other.Id)&lt;br /&gt;
            return -1;&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Program {&lt;br /&gt;
    static void Main() {&lt;br /&gt;
        Tree&amp;lt;Employee&amp;gt; empTree = new Tree&amp;lt;Employee&amp;gt;(new Employee { Id = 1, FirstName = &amp;quot;Janet&amp;quot;, LastName = &amp;quot;Gates&amp;quot;, Department = &amp;quot;IT&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 2, FirstName = &amp;quot;O&amp;quot;, LastName = &amp;quot;G&amp;quot;, Department = &amp;quot;Marketing&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 4, FirstName = &amp;quot;K&amp;quot;, LastName = &amp;quot;H&amp;quot;, Department = &amp;quot;IT&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 6, FirstName = &amp;quot;L&amp;quot;, LastName = &amp;quot;H&amp;quot;, Department = &amp;quot;Sales&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 3, FirstName = &amp;quot;E&amp;quot;, LastName = &amp;quot;L&amp;quot;, Department = &amp;quot;Sales&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 5, FirstName = &amp;quot;D&amp;quot;, LastName = &amp;quot;L&amp;quot;, Department = &amp;quot;Marketing&amp;quot; });&lt;br /&gt;
        var depts = empTree.Select(d =&amp;gt; d.Department).Distinct();&lt;br /&gt;
        foreach (var dept in depts)&lt;br /&gt;
            Console.WriteLine(&amp;quot;Department: {0}&amp;quot;, dept);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use Contains, Length and ToUpper from Linq==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        string[] names = { &amp;quot;Tom&amp;quot;, &amp;quot;Dick&amp;quot;, &amp;quot;Harry&amp;quot;, &amp;quot;Mary&amp;quot;, &amp;quot;Jay&amp;quot; };&lt;br /&gt;
        IEnumerable&amp;lt;string&amp;gt; filtered = names.Where(n =&amp;gt; n.Contains(&amp;quot;a&amp;quot;));&lt;br /&gt;
        IEnumerable&amp;lt;string&amp;gt; sorted = filtered.OrderBy(n =&amp;gt; n.Length);&lt;br /&gt;
        IEnumerable&amp;lt;string&amp;gt; finalQuery = sorted.Select(n =&amp;gt; n.ToUpper());&lt;br /&gt;
        foreach (string name in filtered)&lt;br /&gt;
            Console.Write(name + &amp;quot;|&amp;quot;);        &lt;br /&gt;
        Console.WriteLine();&lt;br /&gt;
        foreach (string name in sorted)&lt;br /&gt;
            Console.Write(name + &amp;quot;|&amp;quot;);        &lt;br /&gt;
        Console.WriteLine();&lt;br /&gt;
        foreach (string name in finalQuery)&lt;br /&gt;
            Console.Write(name + &amp;quot;|&amp;quot;);        &lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use from where select to choose the Employees in the IT department from a tree==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class Tree&amp;lt;TreeNode&amp;gt; : IEnumerable&amp;lt;TreeNode&amp;gt; where TreeNode : IComparable&amp;lt;TreeNode&amp;gt; {&lt;br /&gt;
    public Tree(TreeNode nodeValue) {&lt;br /&gt;
        this.NodeData = nodeValue;&lt;br /&gt;
        this.LeftTree = null;&lt;br /&gt;
        this.RightTree = null;&lt;br /&gt;
    }&lt;br /&gt;
    public void Insert(TreeNode newItem) {&lt;br /&gt;
        TreeNode currentNodeValue = this.NodeData;&lt;br /&gt;
        if (currentNodeValue.rupareTo(newItem) &amp;gt; 0) {&lt;br /&gt;
            if (this.LeftTree == null) {&lt;br /&gt;
                this.LeftTree = new Tree&amp;lt;TreeNode&amp;gt;(newItem);&lt;br /&gt;
            } else {&lt;br /&gt;
                this.LeftTree.Insert(newItem);&lt;br /&gt;
            }&lt;br /&gt;
        } else {&lt;br /&gt;
            if (this.RightTree == null) {&lt;br /&gt;
                this.RightTree = new Tree&amp;lt;TreeNode&amp;gt;(newItem);&lt;br /&gt;
            } else {&lt;br /&gt;
                this.RightTree.Insert(newItem);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public void WalkTree() {&lt;br /&gt;
        if (this.LeftTree != null) {&lt;br /&gt;
            this.LeftTree.WalkTree();&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(this.NodeData.ToString());&lt;br /&gt;
        if (this.RightTree != null) {&lt;br /&gt;
            this.RightTree.WalkTree();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public TreeNode NodeData { get; set; }&lt;br /&gt;
    public Tree&amp;lt;TreeNode&amp;gt; LeftTree { get; set; }&lt;br /&gt;
    public Tree&amp;lt;TreeNode&amp;gt; RightTree { get; set; }&lt;br /&gt;
&lt;br /&gt;
    IEnumerator&amp;lt;TreeNode&amp;gt; IEnumerable&amp;lt;TreeNode&amp;gt;.GetEnumerator() {&lt;br /&gt;
        if (this.LeftTree != null) {&lt;br /&gt;
            foreach (TreeNode item in this.LeftTree) {&lt;br /&gt;
                yield return item;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        yield return this.NodeData;&lt;br /&gt;
        if (this.RightTree != null) {&lt;br /&gt;
            foreach (TreeNode item in this.RightTree) {&lt;br /&gt;
                yield return item;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {&lt;br /&gt;
        throw new NotImplementedException();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Employee : IComparable&amp;lt;Employee&amp;gt; {&lt;br /&gt;
    public string FirstName { get; set; }&lt;br /&gt;
    public string LastName { get; set; }&lt;br /&gt;
    public string Department { get; set; }&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public override string ToString() {&lt;br /&gt;
        return String.Format(&amp;quot;Id: {0}, Name: {1} {2}, Dept: {3}&amp;quot;, this.Id, this.FirstName, this.LastName, this.Department);&lt;br /&gt;
    }&lt;br /&gt;
    int IComparable&amp;lt;Employee&amp;gt;.rupareTo(Employee other) {&lt;br /&gt;
        if (other == null)&lt;br /&gt;
            return 1;&lt;br /&gt;
        if (this.Id &amp;gt; other.Id)&lt;br /&gt;
            return 1;&lt;br /&gt;
        if (this.Id &amp;lt; other.Id)&lt;br /&gt;
            return -1;&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Program {&lt;br /&gt;
    static void Main() {&lt;br /&gt;
        Tree&amp;lt;Employee&amp;gt; empTree = new Tree&amp;lt;Employee&amp;gt;(new Employee { Id = 1, FirstName = &amp;quot;Janet&amp;quot;, LastName = &amp;quot;Gates&amp;quot;, Department = &amp;quot;IT&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 2, FirstName = &amp;quot;O&amp;quot;, LastName = &amp;quot;G&amp;quot;, Department = &amp;quot;Marketing&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 4, FirstName = &amp;quot;K&amp;quot;, LastName = &amp;quot;H&amp;quot;, Department = &amp;quot;IT&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 6, FirstName = &amp;quot;L&amp;quot;, LastName = &amp;quot;H&amp;quot;, Department = &amp;quot;Sales&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 3, FirstName = &amp;quot;E&amp;quot;, LastName = &amp;quot;L&amp;quot;, Department = &amp;quot;Sales&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 5, FirstName = &amp;quot;D&amp;quot;, LastName = &amp;quot;L&amp;quot;, Department = &amp;quot;Marketing&amp;quot; });&lt;br /&gt;
        var ITEmployees = from e in empTree&lt;br /&gt;
                          where String.Equals(e.Department, &amp;quot;IT&amp;quot;)&lt;br /&gt;
                          select e;&lt;br /&gt;
        foreach (var emp in ITEmployees)&lt;br /&gt;
            Console.WriteLine(emp);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use LINQ to get Employees in the IT department from a tree==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class Tree&amp;lt;TreeNode&amp;gt; : IEnumerable&amp;lt;TreeNode&amp;gt; where TreeNode : IComparable&amp;lt;TreeNode&amp;gt; {&lt;br /&gt;
    public Tree(TreeNode nodeValue) {&lt;br /&gt;
        this.NodeData = nodeValue;&lt;br /&gt;
        this.LeftTree = null;&lt;br /&gt;
        this.RightTree = null;&lt;br /&gt;
    }&lt;br /&gt;
    public void Insert(TreeNode newItem) {&lt;br /&gt;
        TreeNode currentNodeValue = this.NodeData;&lt;br /&gt;
        if (currentNodeValue.rupareTo(newItem) &amp;gt; 0) {&lt;br /&gt;
            if (this.LeftTree == null) {&lt;br /&gt;
                this.LeftTree = new Tree&amp;lt;TreeNode&amp;gt;(newItem);&lt;br /&gt;
            } else {&lt;br /&gt;
                this.LeftTree.Insert(newItem);&lt;br /&gt;
            }&lt;br /&gt;
        } else {&lt;br /&gt;
            if (this.RightTree == null) {&lt;br /&gt;
                this.RightTree = new Tree&amp;lt;TreeNode&amp;gt;(newItem);&lt;br /&gt;
            } else {&lt;br /&gt;
                this.RightTree.Insert(newItem);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public void WalkTree() {&lt;br /&gt;
        if (this.LeftTree != null) {&lt;br /&gt;
            this.LeftTree.WalkTree();&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(this.NodeData.ToString());&lt;br /&gt;
        if (this.RightTree != null) {&lt;br /&gt;
            this.RightTree.WalkTree();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public TreeNode NodeData { get; set; }&lt;br /&gt;
    public Tree&amp;lt;TreeNode&amp;gt; LeftTree { get; set; }&lt;br /&gt;
    public Tree&amp;lt;TreeNode&amp;gt; RightTree { get; set; }&lt;br /&gt;
&lt;br /&gt;
    IEnumerator&amp;lt;TreeNode&amp;gt; IEnumerable&amp;lt;TreeNode&amp;gt;.GetEnumerator() {&lt;br /&gt;
        if (this.LeftTree != null) {&lt;br /&gt;
            foreach (TreeNode item in this.LeftTree) {&lt;br /&gt;
                yield return item;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        yield return this.NodeData;&lt;br /&gt;
        if (this.RightTree != null) {&lt;br /&gt;
            foreach (TreeNode item in this.RightTree) {&lt;br /&gt;
                yield return item;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {&lt;br /&gt;
        throw new NotImplementedException();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Employee : IComparable&amp;lt;Employee&amp;gt; {&lt;br /&gt;
    public string FirstName { get; set; }&lt;br /&gt;
    public string LastName { get; set; }&lt;br /&gt;
    public string Department { get; set; }&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public override string ToString() {&lt;br /&gt;
        return String.Format(&amp;quot;Id: {0}, Name: {1} {2}, Dept: {3}&amp;quot;, this.Id, this.FirstName, this.LastName, this.Department);&lt;br /&gt;
    }&lt;br /&gt;
    int IComparable&amp;lt;Employee&amp;gt;.rupareTo(Employee other) {&lt;br /&gt;
        if (other == null)&lt;br /&gt;
            return 1;&lt;br /&gt;
        if (this.Id &amp;gt; other.Id)&lt;br /&gt;
            return 1;&lt;br /&gt;
        if (this.Id &amp;lt; other.Id)&lt;br /&gt;
            return -1;&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Program {&lt;br /&gt;
    static void Main() {&lt;br /&gt;
        Tree&amp;lt;Employee&amp;gt; empTree = new Tree&amp;lt;Employee&amp;gt;(new Employee { Id = 1, FirstName = &amp;quot;Janet&amp;quot;, LastName = &amp;quot;Gates&amp;quot;, Department = &amp;quot;IT&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 2, FirstName = &amp;quot;O&amp;quot;, LastName = &amp;quot;G&amp;quot;, Department = &amp;quot;Marketing&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 4, FirstName = &amp;quot;K&amp;quot;, LastName = &amp;quot;H&amp;quot;, Department = &amp;quot;IT&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 6, FirstName = &amp;quot;L&amp;quot;, LastName = &amp;quot;H&amp;quot;, Department = &amp;quot;Sales&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 3, FirstName = &amp;quot;E&amp;quot;, LastName = &amp;quot;L&amp;quot;, Department = &amp;quot;Sales&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 5, FirstName = &amp;quot;D&amp;quot;, LastName = &amp;quot;L&amp;quot;, Department = &amp;quot;Marketing&amp;quot; });&lt;br /&gt;
        var ITEmployees = empTree.Where(e =&amp;gt; String.Equals(e.Department, &amp;quot;IT&amp;quot;)).Select(emp =&amp;gt; emp);&lt;br /&gt;
        foreach (var emp in ITEmployees)&lt;br /&gt;
            Console.WriteLine(emp);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use select to retrieve all nodes in a Binary tree==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class Tree&amp;lt;TreeNode&amp;gt; : IEnumerable&amp;lt;TreeNode&amp;gt; where TreeNode : IComparable&amp;lt;TreeNode&amp;gt; {&lt;br /&gt;
    public Tree(TreeNode nodeValue) {&lt;br /&gt;
        this.NodeData = nodeValue;&lt;br /&gt;
        this.LeftTree = null;&lt;br /&gt;
        this.RightTree = null;&lt;br /&gt;
    }&lt;br /&gt;
    public void Insert(TreeNode newItem) {&lt;br /&gt;
        TreeNode currentNodeValue = this.NodeData;&lt;br /&gt;
        if (currentNodeValue.rupareTo(newItem) &amp;gt; 0) {&lt;br /&gt;
            if (this.LeftTree == null) {&lt;br /&gt;
                this.LeftTree = new Tree&amp;lt;TreeNode&amp;gt;(newItem);&lt;br /&gt;
            } else {&lt;br /&gt;
                this.LeftTree.Insert(newItem);&lt;br /&gt;
            }&lt;br /&gt;
        } else {&lt;br /&gt;
            if (this.RightTree == null) {&lt;br /&gt;
                this.RightTree = new Tree&amp;lt;TreeNode&amp;gt;(newItem);&lt;br /&gt;
            } else {&lt;br /&gt;
                this.RightTree.Insert(newItem);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public void WalkTree() {&lt;br /&gt;
        if (this.LeftTree != null) {&lt;br /&gt;
            this.LeftTree.WalkTree();&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(this.NodeData.ToString());&lt;br /&gt;
        if (this.RightTree != null) {&lt;br /&gt;
            this.RightTree.WalkTree();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public TreeNode NodeData { get; set; }&lt;br /&gt;
    public Tree&amp;lt;TreeNode&amp;gt; LeftTree { get; set; }&lt;br /&gt;
    public Tree&amp;lt;TreeNode&amp;gt; RightTree { get; set; }&lt;br /&gt;
&lt;br /&gt;
    IEnumerator&amp;lt;TreeNode&amp;gt; IEnumerable&amp;lt;TreeNode&amp;gt;.GetEnumerator() {&lt;br /&gt;
        if (this.LeftTree != null) {&lt;br /&gt;
            foreach (TreeNode item in this.LeftTree) {&lt;br /&gt;
                yield return item;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        yield return this.NodeData;&lt;br /&gt;
        if (this.RightTree != null) {&lt;br /&gt;
            foreach (TreeNode item in this.RightTree) {&lt;br /&gt;
                yield return item;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {&lt;br /&gt;
        throw new NotImplementedException();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Employee : IComparable&amp;lt;Employee&amp;gt; {&lt;br /&gt;
    public string FirstName { get; set; }&lt;br /&gt;
    public string LastName { get; set; }&lt;br /&gt;
    public string Department { get; set; }&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public override string ToString() {&lt;br /&gt;
        return String.Format(&amp;quot;Id: {0}, Name: {1} {2}, Dept: {3}&amp;quot;, this.Id, this.FirstName, this.LastName, this.Department);&lt;br /&gt;
    }&lt;br /&gt;
    int IComparable&amp;lt;Employee&amp;gt;.rupareTo(Employee other) {&lt;br /&gt;
        if (other == null)&lt;br /&gt;
            return 1;&lt;br /&gt;
        if (this.Id &amp;gt; other.Id)&lt;br /&gt;
            return 1;&lt;br /&gt;
        if (this.Id &amp;lt; other.Id)&lt;br /&gt;
            return -1;&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class Program {&lt;br /&gt;
    static void Main() {&lt;br /&gt;
        Tree&amp;lt;Employee&amp;gt; empTree = new Tree&amp;lt;Employee&amp;gt;(new Employee { Id = 1, FirstName = &amp;quot;Janet&amp;quot;, LastName = &amp;quot;Gates&amp;quot;, Department = &amp;quot;IT&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 2, FirstName = &amp;quot;O&amp;quot;, LastName = &amp;quot;G&amp;quot;, Department = &amp;quot;Marketing&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 4, FirstName = &amp;quot;K&amp;quot;, LastName = &amp;quot;H&amp;quot;, Department = &amp;quot;IT&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 6, FirstName = &amp;quot;L&amp;quot;, LastName = &amp;quot;H&amp;quot;, Department = &amp;quot;Sales&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 3, FirstName = &amp;quot;E&amp;quot;, LastName = &amp;quot;L&amp;quot;, Department = &amp;quot;Sales&amp;quot; });&lt;br /&gt;
        empTree.Insert(new Employee { Id = 5, FirstName = &amp;quot;D&amp;quot;, LastName = &amp;quot;L&amp;quot;, Department = &amp;quot;Marketing&amp;quot; });&lt;br /&gt;
        var allEmployees = from e in empTree.ToList&amp;lt;Employee&amp;gt;()&lt;br /&gt;
                           select e;&lt;br /&gt;
        foreach (var emp in allEmployees)&lt;br /&gt;
            Console.WriteLine(emp);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>