如何使LINQ to SQL中的特定列与众不同 [英] How to get particular column distinct in LINQ to SQL

查看:51
本文介绍了如何使LINQ to SQL中的特定列与众不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将列作为类别,并在表格中为每个类别添加歌曲.几乎有10首歌曲,总共有7个类别,因此被列为

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

类别1 类别2 类别3 类别4

category1 category2 category3 category4

我尝试过:

(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.

推荐答案

当您需要获取不同的行时,您只需要提供要从中获取不同数据的列即可.

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();

这篇关于如何使LINQ to SQL中的特定列与众不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆