Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there way to use Distinct in LINQ query syntax?

Tags:

c#

linq

Is there way to rewrite:

var tbl = ds.TABLES; var q = from c in tbl         select c.TABLE_TYPE; string s = ""; foreach (var item in q.Distinct()) {     s += "[" + item + "]"; }         MessageBox.Show(s); 

So that the Distinct() call is in the LINQ query?

like image 895
Aaron Anodide Avatar asked Apr 19 '11 18:04

Aaron Anodide


People also ask

Why distinct is not working in LINQ?

LINQ Distinct is not that smart when it comes to custom objects. All it does is look at your list and see that it has two different objects (it doesn't care that they have the same values for the member fields). One workaround is to implement the IEquatable interface as shown here.

How do I get distinct on a single column in LINQ?

distinct in Linq to get result based on one field of the table (so do not require a whole duplicated records from table). I know writing basic query using distinct as followed: var query = (from r in table1 orderby r. Text select r).


1 Answers

There is no Distinct() method syntax in the language integrated query syntax. The closest you could do would be to move the current call:

var q = (from c in tbl          select c.TABLE_TYPE).Distinct(); 
like image 136
Reed Copsey Avatar answered Oct 31 '22 00:10

Reed Copsey