【发布时间】:2013-03-16 19:53:33
【问题描述】:
自从我上次使用内部连接以来,我已经有好几年了,所以我有点生疏了。 它有 3 张桌子
相册、相册图片和用户
现在首先在我的存储库中,i Inner Left Join Album 和 AlbumsImages,问题是我只想要从 Albumsimages 中的第一个输入,按先 Cover Desc 然后 Id Desc 排序(AlbumImages 中可以有 0 个图像!)。之后,我加入我的用户表,在相册中的用户 ID 到用户 ID。 我的问题是我不仅得到 1 张专辑,而且得到了 AlbumsImages 中每个图像的结果,我只想要 1 张。 我在这里做错了什么?
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();
测试:
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,
});
【问题讨论】:
标签: c# linq linq-to-sql repository-pattern