72

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?

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121

6 Answers6

78

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();
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 12
    an additional info; the query above will create following sql `select distinct c.TABLE_TYPE from tbl c` when used against sql, so do not worry about performance issues. – Erdogan Kurtur Mar 18 '13 at 15:50
64

The Distinct extension method in LINQ does not have a query syntax equivalent.

See https://learn.microsoft.com/en-us/archive/blogs/charlie/linq-farm-using-distinct-and-avoiding-lambdas for additional information as to why.

jmoreno
  • 12,752
  • 4
  • 60
  • 91
Thebigcheeze
  • 3,408
  • 2
  • 22
  • 18
  • The link is dead now - do you know where I can find post? I'd be interested in understanding why this is. – EJoshuaS - Stand with Ukraine Feb 03 '20 at 15:06
  • @EJoshuaS-ReinstateMonica here's a link to an archive of the article :) https://web.archive.org/web/20160216044315/http://blogs.msdn.com:80/b/charlie/archive/2006/11/19/linq-farm-group-and-distinct.aspx – Andrew Cameron Mar 06 '20 at 14:52
16
(from c in tbl select c.TABLE_TYPE).Distinct();
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
Matt
  • 3,143
  • 1
  • 17
  • 14
  • 1
    I guess the OP who wanted to replace `var item in q.Distinct()` by the query syntax was already aware of this. It's amazing a post which doesn't answer the question can get 14 votes! – mins May 17 '20 at 08:42
2

VB has this functionality if you place the distinct after select.

Anthony Wood
  • 395
  • 1
  • 3
  • 16
2

You may capture HashSet and put where clause before select:

var hs = new HashSet<char>();

from c in "abcabcd"
where hs.Add(c)
select c;
Kuba
  • 457
  • 3
  • 13
  • 1
    Clever. Alas there is a need to declare the HashSet beforehand which defeat the self-containment quality of the query expression. – Frederic Feb 28 '20 at 12:09
0

In the search for a Distinct-function for LINQ upon finding this question and realizing it doesn't exist, my workaround is by using GroupBy(). The obvious problem is that the distinct-set doesn't contain all the data (say you have three fields but only want to distinct on two fields missing out on the value for the last field, but, then again, DISTINCT in t-sql works the same way).

LINQ-code (hence the Dump):

void Main()
{
    var gt = new GenerateThings();
    var dlist = gt.list();
    dlist.Dump();

    dlist.GroupBy(x => new {x.id, x.cat}).Dump();
}

public class model
{
    public int id {get;set;}
    public int cat {get;set;}
    public int type {get;set;}
}

public class GenerateThings
{
    public List<model>list()
    {
        var dlist = new List<model>();
        dlist.Add(createNew(1, 1, 1));
        dlist.Add(createNew(1, 1, 1));
        dlist.Add(createNew(1, 2, 1));
        dlist.Add(createNew(1, 2, 1));
        dlist.Add(createNew(1, 1, 2));
        dlist.Add(createNew(1, 1, 2));
        dlist.Add(createNew(1, 1, 2));
        return dlist;
    }
    private model createNew(int id, int cat, int type){
        return new model{
            id = id,
            cat = cat,
            type = type
        };
    }
}

LINQ-dump result