LINQ区别于3个表(多对多关系)

时间:2016-06-12 13:10:05

标签: c# entity-framework linq

第一次在这里发帖,但很长一段时间潜伏着:)

我是LINQ和Entity框架的新手,我只是无法解决这个问题,我知道SQL并且会在几分钟内在常规sql中编写这样的查询,但这让我很头疼。 / p>

所以我有3张桌子:

$stmt->bindParam(':id', $id, PDO::PARAM_NULL);

现在多个电影可以多次在同一个播放列表中,我可以获得这些,但是如果我想要所有电影但是有不同的播放列表我将如何进行,以便我可以显示电影所属的播放列表没有重复?

感谢您的帮助

编辑: 忘了提我的函数返回IQueryable<电影>

Json我得到:

public partial class movie
{
    public int movie_id { get; set; }
    public string movie_name { get; set; }

    public virtual ICollection<playlist_movie> playlist_movie { get; set; }
}

public partial class playlist
{
    public int playlist_id { get; set; }
    public string playlist_name { get; set; }

    public virtual ICollection<playlist_movie> playlist_movie { get; set; }
}

public partial class playlist_movie
{
    public int id { get; set; }
    public int playlist_id { get; set; }
    public int movie_id { get; set; }
    public virtual playlist playlist { get; set; }
    public virtual movie movie { get; set; }
}
我希望Json:

{  
  "playlist_movie":[  
     {  
        "playlist":{  
           "playlist_id":1,
           "playlist_name":"test playlist"
        }
     },
     {  
        "playlist":{  
           "playlist_id":1,
           "playlist_name":"test playlist"
        }
     },
     {  
        "playlist":{  
           "playlist_id":5,
           "playlist_name":"testtest"
        }
     }
  ],
  "movie_id":5,
  "movie_name":"Test Movie"
}

1 个答案:

答案 0 :(得分:0)

在这种情况下,您不需要使用Distinct。您只需使用GroupBy playlist_id并获取第一个列表。见下面的代码

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<playlist_movie> lists = new List<playlist_movie>() {
                new playlist_movie() {
                    playlist_id = 1,
                    movie_id = 5,
                    movie = new movie() { movie_id = 5, movie_name = "Test Movie"},
                    playlist = new playlist() { playlist_name = "test playlist", playlist_id = 1}
                },
                new playlist_movie() {
                    playlist_id = 1,
                    movie_id = 5,
                    movie = new movie() { movie_id = 5, movie_name = "Test Movie"},
                    playlist = new playlist() { playlist_name = "test playlist", playlist_id = 1}
                },
                new playlist_movie() {
                    playlist_id = 5,
                    movie_id = 5,
                    movie = new movie() { movie_id = 5, movie_name = "Test Movie"},
                    playlist = new playlist() { playlist_name = "testtest", playlist_id = 5}
                }


            };

            string moveie_name = "Test Movie";

            var results = lists.Where(x => x.movie.movie_name == moveie_name)
                .Select(x => new { moveie_name = x.movie.movie_name, movie_id = x.movie_id, playlist_id = x.playlist_id, playlist_name = x.playlist.playlist_name })
                .GroupBy(x => x.playlist_id)
                .Select(x => x.FirstOrDefault()).ToList();

        }
    }
    public partial class movie
    {
        public int movie_id { get; set; }
        public string movie_name { get; set; }

        public virtual ICollection<playlist_movie> playlist_movie { get; set; }
    }

    public partial class playlist
    {
        public int playlist_id { get; set; }
        public string playlist_name { get; set; }

        public virtual ICollection<playlist_movie> playlist_movie { get; set; }
    }

    public partial class playlist_movie
    {
        public int id { get; set; }
        public int playlist_id { get; set; }
        public int movie_id { get; set; }
        public virtual playlist playlist { get; set; }
        public virtual movie movie { get; set; }
    }
}