Search
Close this search box.

Using LINQ Distinct: With an Example on ASP.NET MVC SelectListItem

One of the things that might be surprising in the LINQ Distinct standard query operator is that it doesn’t automatically work properly on custom classes. There are reasons for this, which I’ll explain shortly. The example I’ll use in this post focuses on pulling a unique list of names to load into a drop-down list. I’ll explain the sample application, show you typical first shot at Distinct, explain why it won’t work as you expect, and then demonstrate a solution to make Distinct work with any custom class.

The technologies I’m using are  LINQ to TwitterLINQ to ObjectsTelerik Extensions for ASP.NET MVCASP.NET MVC 2, and Visual Studio 2010.

The function of the example program is to show a list of people that I follow.  In Twitter API vernacular, these people are called “Friends”; though I’ve never met most of them in real life. This is part of the ubiquitous language of social networking, and Twitter in particular, so you’ll see my objects named accordingly. Where Distinct comes into play is because I want to have a drop-down list with the names of the friends appearing in the list. Some friends are quite verbose, which means I can’t just extract names from each tweet and populate the drop-down; otherwise, I would end up with many duplicate names. Therefore, Distinct is the appropriate operator to eliminate the extra entries from my friends who tend to be enthusiastic tweeters. The sample doesn’t do anything with the drop-down list and I leave that up to imagination for what it’s practical purpose could be; perhaps a filter for the list if I only want to see a certain person’s tweets or maybe a quick list that I plan to combine with a TextBox and Button to reply to a friend. When the program runs, you’ll need to authenticate with Twitter, because I’m using OAuth (DotNetOpenAuth), for authentication, and then you’ll see the drop-down list of names above the grid with the most recent tweets from friends. Here’s what the application looks like when it runs:

As you can see, there is a drop-down list above the grid. The drop-down list is where most of the focus of this article will be. There is some description of the code before we talk about the Distinct operator, but we’ll get there soon.

This is an ASP.NET MVC2 application, written with VS 2010. Here’s the View that produces this screen:

01 < % @ Page Language = "C#" MasterPageFile =
    "~/Views/Shared/Site.Master" Inherits =
        "System.Web.Mvc.ViewPage<TwitterFriendsViewModel>" % > 02 <
        % @ Import Namespace = "DistinctSelectList.Models" % > 03

                              04 < asp
    : Content ID = "Content1" ContentPlaceHolderID = "TitleContent" runat =
                                  "server" > 05 Home Page 06 < / asp
    : Content > <asp : Content ID = "Content2" ContentPlaceHolderID =
                     "MainContent" runat = "server"> 07 < fieldset > 08 <
                                  legend > Twitter Friends</ legend> 09 < div >
                                  10 < %=
    Html.DropDownListFor(11 twendVM => twendVM.FriendNames,
                         12 Model.FriendNames, 13 "<All Friends>") %
    > 14 < / div > 15 < div > 16 <
    % Html.Telerik()
            .Grid<TweetViewModel>(Model.Tweets) 17.Name("TwitterFriendsGrid") 18
            .Columns(cols => 19 {
              20 cols.Template(
                  col => 21 { % > 22 < img src = "<%= col.ImageUrl %>" 23 alt =
                                  "<%= col.ScreenName %>" / > 24 < % });
              25 cols.Bound(col => col.ScreenName);
              26 cols.Bound(col => col.Tweet);
              27
            }) 28.Render();
% > 29 < / div > 30 < / fieldset > 31 < / asp : Content >

As shown above, the Grid is from Telerik’s Extensions for ASP.NET MVC. The first column is a template that renders the user’s Avatar from a URL provided by the Twitter query. Both the Grid and DropDownListFor display properties that are collections from a TwitterFriendsViewModel class, shown below:

01 using System.Collections.Generic;
02 using System.Web.Mvc;
03

    04 namespace DistinctSelectList.Models 05 {
  06
      /// <summary>
      07
      /// For finding friend info on screen
      08
      /// </summary>
      09 public class TwitterFriendsViewModel
  10 {
    11
        /// <summary>
        12
        /// Display names of friends in drop-down list
        13
        /// </summary>
        14 public List<SelectListItem> FriendNames {
          get; set;
        }
    15

        16
        /// <summary>
        17
        /// Display tweets in grid
        18
        /// </summary>
        19 public List<TweetViewModel> Tweets {
          get; set;
        }
    20
  }
  21
}

I created the TwitterFreindsViewModel. The two Lists are what the View consumes to populate the DropDownListFor and Grid. Notice that FriendNames is a List of SelectListItem, which is an MVC class. Another custom class I created is the TweetViewModel (the type of the Tweets List), shown below:

01 namespace DistinctSelectList.Models 02 {
  03
      /// <summary>
      04
      /// Info on friend tweets
      05
      /// </summary>
      06 public class TweetViewModel
  07 {
    08
        /// <summary>
        09
        /// User's avatar
        10
        /// </summary>
        11 public string ImageUrl {
          get; set;
        }
    12

        13
        /// <summary>
        14
        /// User's Twitter name
        15
        /// </summary>
        16 public string ScreenName {
          get; set;
        }
    17

        18
        /// <summary>
        19
        /// Text containing user's tweet
        20
        /// </summary>
        21 public string Tweet {
          get; set;
        }
    22
  }
  23
}

The initial Twitter query returns much more information than we need for our purposes and this a special class for displaying info in the View.  Now you know about the View and how it’s constructed. Let’s look at the controller next.

The controller for this demo performs authentication, data retrieval, data manipulation, and view selection. I’ll skip the description of the authentication because it’s a normal part of using OAuth with LINQ to Twitter. Instead, we’ll drill down and focus on the Distinct operator. However, I’ll show you the entire controller, below,  so that you can see how it all fits together:

01 using System.Linq;
02 using System.Web.Mvc;
03 using DistinctSelectList.Models;
04 using LinqToTwitter;
05

    06 namespace DistinctSelectList.Controllers 07 {
  08 [HandleError] 09 public class HomeController : Controller 10 {
    11 private MvcOAuthAuthorization auth;
    12 private TwitterContext twitterCtx;
    13

        14
        /// <summary>
        15
        /// Display a list of friends current tweets
        16
        /// </summary>
        17
        /// <returns></returns>
        18 public ActionResult Index() 19 {
      20 auth = new MvcOAuthAuthorization(21 InMemoryTokenManager.Instance,
                                          22 InMemoryTokenManager.AccessToken);
      23

          24 string accessToken = auth.CompleteAuthorize();
      25

          26 if (accessToken != null) 27 {
        28 InMemoryTokenManager.AccessToken = accessToken;
        29
      }
      30

          31 if (auth.CachedCredentialsAvailable) 32 {
        33 auth.SignOn();
        34
      }
      35 else 36 {
        37 return auth.BeginAuthorize();
        38
      }
      39

          40 twitterCtx = new TwitterContext(auth);
41
 
42
            var friendTweets =
43
                (from tweet in twitterCtx.Status
44
                 where tweet.Type == StatusType.Friends
45
                 select new TweetViewModel
46
                 {
47
                     ImageUrl = tweet.User.ProfileImageUrl,
48
                     ScreenName = tweet.User.Identifier.ScreenName,
49
                     Tweet = tweet.Text
50
                })
51
                .ToList();
52

    53 var friendNames =
        54(from tweet in friendTweets 55 select new SelectListItem 56 {
          57 Text = tweet.ScreenName, 58 Value = tweet.ScreenName 59
        })60.Distinct(new SelectListItemComparer()) 61.ToList();
62

    63 var twendsVM =
        new TwitterFriendsViewModel 64 { 65 Tweets = friendTweets,
                                         66 FriendNames = friendNames 67 };
68

    69 return View(twendsVM);
70
    }
    71

        72 public ActionResult About() 73 {
      74 return View();
      75
    }
    76
  }
  77
}

The important part of the listing above are the LINQ to Twitter queries for friendTweets and friendNames. Both of these results are used in the subsequent population of the twendsVM instance that is passed to the view. Let’s dissect these two statements for clarification and focus on what is happening with Distinct.

The query for friendTweets gets a list of the 20 most recent tweets (as specified by the Twitter API for friend queries) and performs a projection into the custom TweetViewModel class, repeated below for your convenience:

01
var friendTweets =
02
    (from tweet in twitterCtx.Status
03
     where tweet.Type == StatusType.Friends
04
     select new TweetViewModel
05
     {
06
         ImageUrl = tweet.User.ProfileImageUrl,
07
         ScreenName = tweet.User.Identifier.ScreenName,
08
         Tweet = tweet.Text
09
     })
10
     .ToList();

The LINQ to Twitter query above simplifies what we need to work with in the View and the reduces the amount of information we have to look at in subsequent queries. Given the friendTweets above, the next query performs another projection into an MVC SelectListItem, which is required for binding to the DropDownList.  This brings us to the focus of this blog post, writing a correct query that uses the Distinct operator. The query below uses LINQ to Objects, querying the friendTweets collection to get friendNames:

1 var friendNames = 2(from tweet in friendTweets 3 select new SelectListItem 4 {
                      5 Text = tweet.ScreenName, 6 Value = tweet.ScreenName 7
                    })8.Distinct() 9.ToList();

The above implementation of Distinct seems normal, but it is deceptively incorrect. After running the query above, by executing the application, you’ll notice that the drop-down list contains many duplicates.  This will send you back to the code scratching your head, but there’s a reason why this happens.

To understand the problem, we must examine how Distinct works in LINQ to Objects. Distinct has two overloads: one without parameters, as shown above, and another that takes a parameter of type IEqualityComparer<T>.  In the case above, no parameters, Distinct will call EqualityComparer<T>.Default behind the scenes to make comparisons as it iterates through the list. You don’t have problems with the built-in types, such as stringintDateTime, etc, because they all implement IEquatable<T>. However, many .NET Framework classes, such as SelectListItem, don’t implement IEquatable<T>. So, what happens is that EqualityComparer<T>.Default results in a call to Object.Equals, which performs reference equality on reference type objects.  You don’t have this problem with value types because the default implementation of Object.Equals is bitwise equality. However, most of your projections that use Distinct are on classes, just like the SelectListItem used in this demo application. So, the reason why Distinct didn’t produce the results we wanted was because we used a type that doesn’t define its own equality and Distinct used the default reference equality. This resulted in all objects being included in the results because they are all separate instances in memory with unique references.

As you might have guessed, the solution to the problem is to use the second overload of Distinct that accepts an IEqualityComparer<T> instance. If you were projecting into your own custom type, you could make that type implement IEqualityComparer<T>, but SelectListItem belongs to the .NET Framework Class Library.  Therefore, the solution is to create a custom type to implement IEqualityComparer<T>, as in the SelectListItemComparer class, shown below:

01 using System.Collections.Generic;
02 using System.Web.Mvc;
03

    04 namespace DistinctSelectList.Models 05 {
  06 public class SelectListItemComparer : EqualityComparer<SelectListItem> 07 {
    08 public override bool Equals(SelectListItem x, SelectListItem y) 09 {
      10 return x.Value.Equals(y.Value);
      11
    }
    12

        13 public override int GetHashCode(SelectListItem obj) 14 {
      15 return obj.Value.GetHashCode();
      16
    }
    17
  }
  18
}

The SelectListItemComparer class above doesn’t implement IEqualityComparer<SelectListItem>, but rather derives from EqualityComparer<SelectListItem>. Microsoft recommends this approach for consistency with the behavior of generic collection classes. However, if your custom type already derives from a base class, go ahead and implement IEqualityComparer<T>, which will still work.

EqualityComparer is an abstract class, that implements IEqualityComparer<T> with Equals and GetHashCode abstract methods. For the purposes of this application, the SelectListItem.Value property is sufficient to determine if two items are equal.   Since SelectListItem.Value is type string, the code delegates equality to the string class. The code also delegates the GetHashCode operation to the string class.You might have other criteria in your own object and would need to define what it means for your object to be equal.

Now that we have an IEqualityComparer<SelectListItem>, let’s fix the problem. The code below modifies the query where we want distinct values:

1 var friendNames = 2(from tweet in friendTweets 3 select new SelectListItem 4 {
                      5 Text = tweet.ScreenName, 6 Value = tweet.ScreenName 7
                    })8.Distinct(new SelectListItemComparer()) 9.ToList();

Notice how the code above passes a new instance of SelectListItemComparer as the parameter to the Distinct operator. Now, when you run the application, the drop-down list will behave as you expect, showing only a unique set of names.

In addition to Distinct, other LINQ Standard Query Operators have overloads that accept IEqualityComparer<T>’s, You can use the same techniques as shown here, with SelectListItemComparer, with those other operators as well. Now you know how to resolve problems with getting Distinct to work properly and also have a way to fix problems with other operators that require equality comparisons.

@JoeMayo

This article is part of the GWB Archives. Original Author: Joe Mayo

Related Posts