2
自從我上次使用內連接後,我已經有多年了,所以我有點生疏。 它有3個表Linq內部左連接返回結果太多
相冊,AlbumsImages和用戶
現在首先在我的倉庫我內左加入專輯和AlbumsImages,事情是我唯一的冷杉封面說明,然後用id DESC要從albumsimages爲了第一entery (在AlbumImages中可以有0張圖片!)。在那之後,我是否會加入我的用戶表,在相冊中的用戶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,
});
是的,我忘了補充一點,一張專輯可以有0個圖像,我還是想有出專輯在該實例 – Androme 2013-03-26 21:17:56
@DoomStone然後用'DefaultIfEmpty' – mattytommo 2013-03-26 21:19:09
我改變你執行'into'第二查詢不明白,你想要將用戶表加入一個into和DefaultIfEmpty?總會有一個給定用戶ID的用戶! – Androme 2013-03-26 22:29:04