Linq to Entities many to many select query

后端 未结 4 1386
独厮守ぢ
独厮守ぢ 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:17

    This proved to be much simpler than it seemed. I've solved the problem using the following blogpost: http://weblogs.asp.net/salimfayad/archive/2008/07/09/linq-to-entities-join-queries.aspx

    The key to this solution is to apply the filter of the bandname on a subset of Bands of the musicstyle collection.

    var result=(from m in _entities.MusicStyle 
                from b in m.Band
                where b.Name.Contains(search)
                select new {
                    BandName = b.Name,
                    m.ID,
                    m.Name,
                    m.Description
                });
    

    notice the line

    from b IN m.Band
    

    This makes sure you are only filtering on bands that have a musicstyle.

    Thanks for your answers but none of them actually solved my problem.

提交回复
热议问题