Linq to Entities many to many select query

后端 未结 4 1387
独厮守ぢ
独厮守ぢ 2020-12-29 06:39

I am at a loss with the following query, which is peanuts in plain T-SQL.

We have three physical tables:

  • Band (PK=BandId)
  • MusicStyle (PK=Muic
4条回答
  •  星月不相逢
    2020-12-29 07:09

    In Linq, actually you don't need to write anything, if you define the relation in the diagram in SQL database, and generated using the utility, the object hierarchy is built automatically. That means, if you do:

    var bands = from ms in db.MusicStyle
                let b = ms.Bands
                where b.Name.Contains(SEARCHSTRING)
                select new {
                    b.Name, ms.Name, 
                    ms.ID, ms.Description};
    

    If you look into the generated classes of entities, the BandMusicStyle should not appear as LINQ to Entities consider that Band and MusicStyle are many to many and that table is not necessary.

    See if it works?

提交回复
热议问题