Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Distinct be expressed using so-called embedded query rather than a method call

Tags:

linq

given the following code:

string[]  colors = {"red","green","blue","red","green","blue"};
var distinctColors = (from c in colors select c).Distinct();
distinctColors.Dump();

Is it possible to fold the call .Distinct() into the embedded query syntax?

something like int T-SQL

select distinct color from TableofColors
like image 841
Ralph Shillington Avatar asked Dec 14 '09 19:12

Ralph Shillington


1 Answers

C#'s query expression syntax doesn't include "distinct". VB's does, however - for example, from the MSDN docs for VB's Distinct clause:

// VB
Dim customerOrders = From cust In customers, ord In orders _
                     Where cust.CustomerID = ord.CustomerID _
                     Select cust.CompanyName, ord.OrderDate _
                     Distinct

The C# equivalent would have to explicitly call Distinct() in dot notation.

However, your example can still be simplified:

string[]  colors = {"red","green","blue","red","green","blue"};
var distinctColors = colors.Distinct();
distinctColors.Dump();

Don't think you have to use query expressions to use LINQ :)

like image 117
Jon Skeet Avatar answered Sep 20 '22 22:09

Jon Skeet