Chinook 数据库-LINQ。我有一个播放列表,我需要写下她的名字、歌曲数量和她的主要流派(该播放列表中的歌曲最多。)

Posted

技术标签:

【中文标题】Chinook 数据库-LINQ。我有一个播放列表,我需要写下她的名字、歌曲数量和她的主要流派(该播放列表中的歌曲最多。)【英文标题】:Chinook Database-LINQ. I have a playlist that i need to write her name,number of songs, and her primary genre(with the most songs in that playlist.) 【发布时间】:2021-09-01 04:29:47 【问题描述】:

我已经完成了歌曲的名称和数量,但流派总是以 System.Collections.Generic.List 的形式出现

 var playlist = _entities.Playlists.Select(x => new PlaylistaDTO
        
            Pesma = x.Name,
            brojPesama = x.Tracks.Count,
            Zanr = x.Tracks.OrderByDescending(y => y.Genre.Tracks.Count).Take(1).Select(z => z.Genre.Name).Distinct()//error is probably this line

        ).ToList();

        foreach (var p in playlist) 
            Console.WriteLine(p);
        

我有一个类 File PlaylistaDTO

enter code here
 public class PlaylistaDTO

    public string Pesma  get; set; 
    public int brojPesama  get; set; 
    public IEnumerable<string> Zanr  get; set; 

    public override string ToString()
    
        return $"Song: Pesma," +
               $"Number of Songs brojPesama" +
               $"Zanr: Zanr";
    

【问题讨论】:

【参考方案1】:

由于您在 PlaylistDTO 中定义 Zanr 的方式,您将获得“System.Collections.Generic.List”。不确定您的 Zanr 包含什么,但您可能想要 .ToString() 它。

假设您有一个实体 Tracks 和一个实体 Playlists,下面的程序应该适合您:

    var playlists = new List<Playlist>();
    playlists.Add(new Playlist()  Name = "Playlist1", TrackName = "Track1" );
    playlists.Add(new Playlist()  Name = "Playlist1", TrackName = "Track2" );
    playlists.Add(new Playlist()  Name = "Playlist1", TrackName = "Track3" );
    var tracks = new List<Track>();
    tracks.Add(new Track()  TrackName = "Track1", Genre = "Genre1" );
    tracks.Add(new Track()  TrackName = "Track2", Genre = "Genre2" );
    tracks.Add(new Track()  TrackName = "Track3", Genre = "Genre2" );


    var playlist = playlists
        .Join(tracks, t => t.TrackName, p => p.TrackName, (p, t) => new  p, t)
        .GroupBy(x => x.p.Name)
        .Select(x => new PlaylistaDTO
        
            Pesma = x.Key,
            brojPesama = x.Count(),
            Zanr = x.GroupBy(y => y.t.Genre).Select(y => new  Genre = y.Key, Count = y.Count() ).OrderByDescending(y => y.Count).FirstOrDefault().Genre
        )
        .ToList();

    foreach (var p in playlist)
    
        Console.WriteLine(p.ToString());
    

public class PlaylistaDTO

    public string Pesma  get; set; 
    public int brojPesama  get; set; 
    public string Zanr  get; set; 

    public override string ToString()
    
        return $"Song: Pesma," +
               $"Number of Songs brojPesama," +
               $"Zanr: Zanr";
    

public class Playlist

    public string Name  get; set; 
    public string TrackName  get; set; 

public class Track

    public string TrackName  get; set; 
    public string Genre  get; set; 

... 将返回:

歌曲:播放列表1,歌曲数量:3,Zanr:流派2

【讨论】:

以上是关于Chinook 数据库-LINQ。我有一个播放列表,我需要写下她的名字、歌曲数量和她的主要流派(该播放列表中的歌曲最多。)的主要内容,如果未能解决你的问题,请参考以下文章

Linq:根据另一个列表分组和求和

使用 LINQ 过滤列表

Linq:列表列表到长列表

LINQ比较两个列表并将结果放入一个列表[重复]

LINQ To Objects - 加入空列表

LINQ 将数据表映射到列表<MyObject>