开发者

LINQ to Entities multiple columns need 1 to be distinct

开发者 https://www.devze.com 2023-03-01 20:46 出处:网络
I am trying to select multiple columns from an entity object but I want 1 property to be distinct.I am very new to both LINQ and Entity Framework so any help will be useful.

I am trying to select multiple columns from an entity object but I want 1 property to be distinct. I am very new to both LINQ and Entity Framework so any help will be useful.

Here is my LINQ query so far:

var listTypes = (from s in context.LIST_OF_VALUES
                 orderby s.SORT_INDEX
                 select new { s.LIST_TYPE, s.DISPLAY_TEXT });

I want s.LIST开发者_运维知识库_TYPE to be distinct. I figure using the groupby keyword is what I want (maybe?) but I have not found a way to use it that works.

Thank you.


Assuming DISPLAY_TEXT matches LIST_TYPE somehow (so you don't lose any information):

var distinct = context.LIST_OF_VALUES
    .OrderBy(s => s.SORT_INDEX)
    .GroupBy(s => s.LIST_TYPE)
    .Select(g => new { g.Key, g.First().DISPLAY_TEXT });
0

精彩评论

暂无评论...
验证码 换一张
取 消