Distinct in Linq to SQL

I was trying to get Linq to SQL to do a Distinct query today, and found that it wasn’t quite so obvious how to do it.

This works: -

From item in table Select field Distinct

This doesn’t: -

From item in table Distinct Select field

Neither does this: -

From item in table Select field.Distinct

Note that all 3 of these appear fine with Intellisense

No related posts.

Tags: , , , ,

One Response to “Distinct in Linq to SQL”

  1. numega says:

    For the 1st expression:
    From item in table Select field Distinct
    it should ‘not’ work

    For the 3rd expression:
    From item in table Select field.Distinct
    -> (From item in table Select field).Distinct()
    should do the work

    Distinct is a filtering method.
    Distinct is a LINQ operator and has no corresponding query syntax.

Leave a Reply