May differ by expression using a so-called inline query rather than a method call

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 drop the .Distinct() call into the inline query syntax?

something like int t-sql

 select distinct color from TableofColors 
+6
linq
source share
4 answers

The C # query expression syntax does not include "excellent". VB, however, for example, from the MSDN docs for the VB 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 should explicitly call Distinct() in dotted notation.

However, your example may be simplified:

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

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

+8
source share

There is no clear built-in query syntax in C # as far as I know. It is as close as possible:

 var distinctColors = (from color in colors select color).Distinct() 
+2
source share

The query understanding syntax does not support the Distinct method.

In your case, you can simply write colors.Distinct() ; you are not doing anything with the query expression.

+1
source share

You can try this

 var dis = from c in colors group c by c; foreach (var cVal in dis) { string s = cVal.Key; } 
0
source share

All Articles