Search code examples
c#linqmany-to-manydatabase-first

Linq-to-sql many-many in database first


I have a database with 2 table Song vs Artist mapping by Mapping_Artist_Song, then i add existing database to models(database first) in my project and now i dont know how to use linq to select song have same id but difference artist in database :

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

Solution

  • For the person who find the solution like me :

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