Martin Heller
Contributing Writer

A Working Distinct LINQ Query in C#

analysis
Jan 30, 20081 min

As I mentioned Monday and in my Visual Studio 2008 review, I've been struggling with the finer points of LINQ queries. In a talkback comment to my review, "CSharper" pointed me at one solution to finding the LINQ equivalent of a SQL "SELECT DISTINCT" query: tack on a LINQ .Distinct() method call to the query, which needs to be enclosed in parentheses. That worked, but it destroyed the sort order created by a pre

As I mentioned Monday and in my Visual Studio 2008 review, I’ve been struggling with the finer points of LINQ queries. In a talkback comment to my review, “CSharper” pointed me at one solution to finding the LINQ equivalent of a SQL “SELECT DISTINCT” query: tack on a LINQ .Distinct() method call to the query, which needs to be enclosed in parentheses.

That worked, but it destroyed the sort order created by a previous orderby clause. Obviously, I also had to learn to use the LINQ .OrderBy() method so that I could apply the sort after the sift. That was a little harder, since .OrderBy() takes a lambda expression, and I wasn’t yet familiar with that new C# syntax.

Nevertheless, I got it working. It isn’t beautiful, but this is really version 1.0 of LINQ.

var qAV = (from ai in db.AppInfos
   join acai in db.AppCatAIs on ai.Prodid equals acai.Prodid
   join ac in db.AppCategories on acai.acatid equals ac.acatid
   where ac.Category == category1 || ac.Category == category2
   select ai)
     .Distinct()
     .OrderBy(n => n.ProductName);
Martin Heller

Martin Heller is a contributing writer at InfoWorld. Formerly a web and Windows programming consultant, he developed databases, software, and websites from his office in Andover, Massachusetts, from 1986 to 2010. From 2010 to August of 2012, Martin was vice president of technology and education at Alpha Software. From March 2013 to January 2014, he was chairman of Tubifi, maker of a cloud-based video editor, having previously served as CEO.

Martin is the author or co-author of nearly a dozen PC software packages and half a dozen Web applications. He is also the author of several books on Windows programming. As a consultant, Martin has worked with companies of all sizes to design, develop, improve, and/or debug Windows, web, and database applications, and has performed strategic business consulting for high-tech corporations ranging from tiny to Fortune 100 and from local to multinational.

Martin’s specialties include programming languages C++, Python, C#, JavaScript, and SQL, and databases PostgreSQL, MySQL, Microsoft SQL Server, Oracle Database, Google Cloud Spanner, CockroachDB, MongoDB, Cassandra, and Couchbase. He writes about software development, data management, analytics, AI, and machine learning, contributing technology analyses, explainers, how-to articles, and hands-on reviews of software development tools, data platforms, AI models, machine learning libraries, and much more.

More from this author