Linq-to-sql 数据库中的多对多优先

Linq-to-sql many-many in database first

我有一个包含 2 table 歌曲与艺术家映射的数据库 Mapping_Artist_Song,然后我将现有数据库添加到项目中的模型(首先是数据库),现在我不知道如何使用 linq select 歌曲在数据库中具有相同的 ID 但不同的艺术家:

Song:             

SongID| SongName
------------------ 
|1      |      A |
|2      |      B |
|3      |      C |
------------------
Artist 
Artist ID| Artist Name
------------------ 
|1      |      D |
|2      |      E |
|3      |      F |
------------------
Mapping by :

MAPPING_Artist _SONG
SongID| Artist ID
------------------ 
|1      |      1 |
|1      |      2 |
|1      |      3 |
|3      |      2 |
------------------

Any help really appreciated

Thanks

问题不清楚,但我认为您正在寻找GroupBy方法。 然后你可以计算有多少艺术家与特定歌曲相关联。

见下方代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication63
{
    class Program
    {

        static void Main(string[] args)
        {
            Song.songs = new List<Song>() {
                new Song() { SongID = 1, SongName = "A"},
                new Song() { SongID = 2, SongName = "B"},
                new Song() { SongID = 3, SongName = "C"}
            };

            Artist.artists = new List<Artist>() {
                new Artist() { ArtistID= 1, ArtistName= "D"},
                new Artist() { ArtistID= 2, ArtistName= "E"},
                new Artist() { ArtistID= 3, ArtistName= "F"}
            };

            MAPPING_Artist.mappings = new List<MAPPING_Artist>() {
             new MAPPING_Artist() { SongID = 1, ArtistID = 1},
             new MAPPING_Artist() { SongID = 1, ArtistID = 2},
             new MAPPING_Artist() { SongID = 1, ArtistID = 3},
             new MAPPING_Artist() { SongID = 3, ArtistID = 2}
            };


            var groups = (from map in MAPPING_Artist.mappings
                          join song in Song.songs on map.SongID equals song.SongID
                          join art in Artist.artists on map.ArtistID equals art.ArtistID 
                          select new { songID = song.SongID,songName = song.SongName, artistname = art.ArtistName})
                          .GroupBy(x => x.songID)
                          .Select(x => new {
                              songID = x.Key,
                              songName = x.FirstOrDefault().songName,
                              artists = x.Select(y => y.artistname).ToList()
                          }).ToList();
        }
    }
    public class Song
    {
        public static List<Song> songs = new List<Song>();
        public int SongID { get; set; }
        public string SongName { get; set; }
    }
    public class Artist
    {
        public static List<Artist> artists = new List<Artist>();
        public int ArtistID { get; set; }
        public string ArtistName { get; set; }
    }
    public class MAPPING_Artist
    {
        public static List<MAPPING_Artist> mappings = new List<MAPPING_Artist>();
        public int SongID { get; set; }
        public int ArtistID { get; set; }
    }
}

对于像我这样找到解决方案的人:

var a = from b in BusinessModels.Songs     
   where b.SongName.Contains("") 
   from c in b.Artists
   select c;