How to get particular column distinct in LINQ to SQL

送分小仙女□ 提交于 2020-01-06 02:49:09

问题


I am having columns as category and songs in my table for each category. There are almost 10 songs and in total there are 7 categories such that it is tabled as

category1 songCategory1a
category1 songCategory1b
category1 songCategory1c
---
---
--
category2 songCategory2a
category2 songCategory2b
category2 songCategory2c
---
---
---
category3 songCategory3a
category3 songCategory3b
category3 songCategory3c
---
---
---

Like that there is a table in which I want to get the result as

category1 category2 category3 category4

I tried:

(from s in _context.db_songs
     select new { s.Song_Name, s.Song_Category }).Distinct().ToList();

But it didn't work. Its resulting as such.


回答1:


When you need to get distinct rows, you need to supply only the column you want get distinct data from:

(from s in _context.db_songs
select s.Song_Category ).Distinct().ToList();



回答2:


Check "LINQ Farm: Using Distinct and Avoiding Lambdas". I don't fully understand what you really want, but maybe this large explanation of distinct in LINQ can help you.



来源:https://stackoverflow.com/questions/2786750/how-to-get-particular-column-distinct-in-linq-to-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!