How can I do SELECT UNIQUE with LINQ?

前端 未结 3 1209
野的像风
野的像风 2020-12-05 12:25

I have a list like this:

Red
Red
Brown
Yellow
Green
Green
Brown
Red
Orange

I am trying to do a SELECT UNIQUE with LINQ, i.e. I want

         


        
相关标签:
3条回答
  • 2020-12-05 12:55

    The Distinct() is going to mess up the ordering, so you'll have to the sorting after that.

    var uniqueColors = 
                   (from dbo in database.MainTable 
                     where dbo.Property == true 
                     select dbo.Color.Name).Distinct().OrderBy(name=>name);
    
    0 讨论(0)
  • 2020-12-05 12:59
    var uniqueColors = (from dbo in database.MainTable 
                        where dbo.Property == true
                        select dbo.Color.Name).Distinct();
    
    0 讨论(0)
  • 2020-12-05 13:19

    Using query comprehension syntax you could achieve the orderby as follows:

    var uniqueColors = (from dbo in database.MainTable
                        where dbo.Property
                        orderby dbo.Color.Name ascending
                        select dbo.Color.Name).Distinct();
    
    0 讨论(0)
提交回复
热议问题