ENTERPRISE

LINQ to Objects : How to Get the Index Position of the Results, How to Remove Duplicate Results

9/19/2012 7:05:27 PM

How to Get the Index Position of the Results

Select and SelectMany expose an overload that surfaces the index position (starting at zero) for each returned element in the Select projection. It is surfaced as an overloaded parameter argument of the selector lambda expression and is only accessible using the extension method query syntax. Listing 1 demonstrates how to access and use the index position value in a Select projection. As shown in Output 1, this example simply adds a ranking number for each select result string.

Listing 1. A zero-based index number is exposed by the Select and SelectMany operators—see Output 1
List<CallLog> callLog = CallLog.SampleData();

var q = callLog.GroupBy(g => g.Number)
               .OrderByDescending(g => g.Count())
               .Select((g, index) => new
               {
                   number = g.Key,
                   rank = index + 1,
                   count = g.Count()
               });

foreach (var c in q)
    Console.WriteLine(
        "Rank {0} - {1}, called {2} times.",
        c.rank, c.number, c.count);

Output 1.
Rank 1 - 885 983 8858, called 6 times.
Rank 2 - 546 607 5462, called 6 times.
Rank 3 - 364 202 3644, called 4 times.
Rank 4 - 603 303 6030, called 4 times.
Rank 5 - 848 553 8487, called 4 times.
Rank 6 - 165 737 1656, called 2 times.
Rank 7 - 278 918 2789, called 2 times.

How to Remove Duplicate Results

The Distinct standard query operator performs the role of returning only unique instances in a sequence. The operator internally keeps track of the elements it has returned and skips the second and subsequent duplicate elements as it returns resulting elements. 

The Distinct operator is not supported in the query expression syntax, so it is often appended to the end of a query using extension method syntax. To demonstrate how it is used, the following code removes duplicate strings. The Console output from this code is

Peter
Paul
Mary
Janet


string[] names = new string[] { "Peter", "Paul",
    "Mary", "Peter", "Paul", "Mary", "Janet" };

var q = (from s in names
         where s.Length > 3
         select s).Distinct();

foreach (var name in q)
    Console.WriteLine(name);
Other  
  •  Integrating Exchange Server 2007 in a Non-Windows Environment : Synchronizing Exchange Server 2007 with Novell eDirectory
  •  Integrating Exchange Server 2007 in a Non-Windows Environment : Synchronizing Directory Information with Microsoft Identity Integration Server (MIIS) 2003
  •  IBM WebSphere Process Server 7 and Enterprise Service Bus 7 : Solution administration tasks (part 2)
  •  IBM WebSphere Process Server 7 and Enterprise Service Bus 7 : Solution administration tasks (part 1) - Performing common tasks using the administrative console
  •  Hardware With An Expiry Date (Part 2)
  •  Hardware With An Expiry Date (Part 1)
  •  Managing SharePoint 2010 Data : Custom Field Types
  •  Managing SharePoint 2010 Data : Content Types
  •  Active Directory Domain Services 2008 : Enable a Group Policy Object Link, Enforce a Group Policy Object Link, Remove the Enforcement of a Group Policy Object Link
  •  Active Directory Domain Services 2008 : Link a Group Policy Object, Remove a Group Policy Object Link, Disable a Group Policy Object Link
  •  Microsoft Dynamics AX 2009 : Building Lookups - Creating a lookup dynamically
  •  Microsoft Dynamics AX 2009 : Building Lookups - Creating an automatic lookup
  •  Introducing Our New Zero-Point
  •  Exchange Server 2010 Administration Overview (part 3) - Using the Graphical Administration Tools, Using the Command-Line Administration Tools
  •  Exchange Server 2010 Administration Overview (part 2) - Exchange Server and Windows,Exchange Server and Active Directory
  •  Exchange Server 2010 Administration Overview (part 1) - Exchange Server 2010 and Your Hardware, Exchange Server 2010 Editions
  •  Touch Screens Are Everywhere
  •  Intel Builds Bridge to Next - Generation Macs
  •  Microsoft vs. Google vs. Apple: Who will win?
  •  Phone Business: The Age Of Convergence
  •  
    Most View
    Looking For A Smartphones – Q1 2013
    ViewSonic PJD7820HD Projector
    Silverlight Recipes : Controls - Displaying Row Details in a DataGrid
    iOS – Tax Time : TapTax, My Tax Return 2012, Income Tax Calculator, ITP
    The Ideal OS (Part 1)
    What Might Have Been
    Microsoft .NET : Design Principles and Patterns - Object-Oriented Design (part 2) - Advanced Principles
    iPhone Mini - A Cheaper iPhone
    How To Transfer Data From SSD To HDD
    Evercool Silent Shark CPU Cooler Review (Part 3)
    Top 10
    Windows Management and Maintenance : The Windows 7 Control Panel (part 11) - Region and Language, System
    Windows Management and Maintenance : The Windows 7 Control Panel (part 10) - Programs and Features
    Windows Management and Maintenance : The Windows 7 Control Panel (part 9) - Notification Area Icons, Performance Information and Tools
    Windows Management and Maintenance : The Windows 7 Control Panel (part 8) - Fonts
    Windows Management and Maintenance : The Windows 7 Control Panel (part 7) - Ease of Access Center
    Windows Management and Maintenance : The Windows 7 Control Panel (part 6) - Devices and Printers
    Windows Management and Maintenance : The Windows 7 Control Panel (part 5) - AutoPlay
    Windows Management and Maintenance : The Windows 7 Control Panel (part 4) - AutoPlay
    Windows Management and Maintenance : The Windows 7 Control Panel (part 3) - Action Center
    Windows Management and Maintenance : The Windows 7 Control Panel (part 2)