C# 使用linq的左内联接返回的结果太多

C# 使用linq的左内联接返回的结果太多,c#,linq,linq-to-sql,repository-pattern,C#,Linq,Linq To Sql,Repository Pattern,自从我上一次使用内部连接以来,我已经工作了很多年,所以我有点生锈了。 它有三张桌子 相册、相册和用户 现在,首先在我的存储库中,我左内加入了Album和AlbumsImages,问题是我只想从AlbumsImages中按第一个顺序输入Cover Desc然后Id Desc(AlbumImages中可以有0个图像!)。之后,我加入我的用户表,在相册中的userid上加入到user中的id。 我的问题是,我并不是只得到一张相册,而是相册图像中每个图像的一个结果,而我只想要一张。 我做错了什么

自从我上一次使用内部连接以来,我已经工作了很多年,所以我有点生锈了。 它有三张桌子

相册、相册和用户

现在,首先在我的存储库中,我左内加入了Album和AlbumsImages,问题是我只想从AlbumsImages中按第一个顺序输入Cover Desc然后Id Desc(AlbumImages中可以有0个图像!)。之后,我加入我的用户表,在相册中的userid上加入到user中的id。 我的问题是,我并不是只得到一张相册,而是相册图像中每个图像的一个结果,而我只想要一张。 我做错了什么

    public IQueryable<Album> Get()
    {
        return (from a in context.Albums
                join i in _imageRepository.Get() on a.Id equals i.AlbumId into albumImages
                from cover in albumImages.DefaultIfEmpty()
                orderby cover.Cover descending, cover.Id ascending 
                select new Album()
                    {
                        Id = a.Id,
                        UserId  = a.UserId,
                        Name  = a.Name,
                        Created  = a.Created,
                        LastEdit  = a.LastEdit,
                        Description  = a.Description,
                        Views  = a.Views,
                        Location  = a.Location,
                        Photoshoot  = a.Photoshoot,
                        Cover = cover,
                    });
    }

var albums = (from a in AlbumRepository.Get()
                            join u in UserRepository.Get() on a.UserId equals u.Id
                            orderby a.Id descending
                            select new AlbumDisplayModel()
                                {
                                    Album = a,
                                    User = u
                                }).ToList();

从相册图像中取下
DefaultIfEmpty
。这将返回相册图像,无论是否存在匹配(左加入)。

是的,我忘了添加一个相册可以有0个图像,我仍然希望以该方式输出相册instance@DoomStone然后将第二个查询更改为执行
并使用
DefaultIfEmpty
我不明白,是否希望用户表与into和DefaultIfEmpty连接?将始终存在一个具有给定用户ID的用户@DoomStone Ah抱歉,您必须重写第一个查询,从i in_imageRepository.Get()
中执行
,然后在相册表上执行join
DefaultIfEmpty
,这样您将始终获得所有相册。我已经尝试执行您说过的操作:如果在我的OP中看到“Test”,它将返回与其他方法相同的结果:(@mattytomo)
    return (from i in _imageRepository.Get()
            join album in context.Albums on i.AlbumId equals album.Id into albums
            from a in albums.DefaultIfEmpty()
            select new Album()
                {
                    Id = a.Id,
                    UserId  = a.UserId,
                    Name  = a.Name,
                    Created  = a.Created,
                    LastEdit  = a.LastEdit,
                    Description  = a.Description,
                    Views  = a.Views,
                    Location  = a.Location,
                    Photoshoot  = a.Photoshoot,
                    Cover = i,
                });