【发布时间】:2020-06-15 22:48:32
【问题描述】:
我正在尝试构建一个用于统计的摘要查询。
我有一个包含以下列的数据表(大约 18000 行):
Artist / Album / file_path (one for each song) / rating /
每个艺术家有 1 个或多个专辑,其中包含歌曲,每首歌曲都有评分
我想得到以下结果:
对于每个艺人ID(比艺人名更可靠),专辑总数、歌曲总数、评分总数等于5。
Artist x / #album / #songs / #rating = 5 / song.first() //in song.first i have access to the file path, it can be any file path from the artist hence the first one.
我已经拉了几个小时的头发,但我无法获得每位艺术家的专辑数量:(这是我迄今为止一直在尝试的:
我有一个查询类:
public class art_detail
{
public string artiste { get; set; }
public string fp { get; set; } // the file_path
public int nbr_album { get; set; }
public int nbr_song { get; set; }
public int nbr_rat5 { get; set; }
}
这是我提出的查询:
var result = from res in Globals.ds.Tables[0].AsEnumerable() // the table
.GroupBy(x => new { art = x.Field<int>("Artist_ID"), alb = x.Field<string>("album") })
.Select(x => new art_detail { artiste = x.Select(p =>p.Field<string>("artiste")).First(), fp = x.Select(p=>p.Field<string>("file_path")).First(), nbr_album = x.Key.alb.Count() })
.OrderBy(x => x.artiste)
select res;
不幸的是,计数完全错误,我不知道如何获得评分 = 5 :(
感谢您的帮助!
编辑: 这是我让它工作的查询:
var table = Globals.ds.Tables[0].AsEnumerable();
var stats = table.GroupBy(x => x.Field<int>("Artist_ID"))
.Select(x => new art_detail
{
artiste = x.Select(p=>p.Field<string>("artiste")).First(),
nbr_album = x.Select(y => y.Field<string>("album")).Distinct().Count(),
fp = x.Select(y => y.Field<string>("file_path")).FirstOrDefault(),
nbr_song = x.Count(),
nbr_rat5 = x.Count(y => y.Field<int>("Rating") == 5)
});
比我想象的要简单:)
【问题讨论】:
-
由于您是按艺术家和专辑分组的,因此一位艺术家和多张专辑将有多个元素。因此,您可能应该仅按艺术家分组,然后获取专辑数量等。
-
如果你想报告每个艺术家的统计数据,你需要使用 GroupBy。因为这就是你正在做的事情。您正在对数据进行分组。如果您只是在寻找一位艺术家,那么您可以通过使用 where 语句和 SelectMany() docs.microsoft.com/en-us/dotnet/api/… 来避免分组