最初に私はこのクエリを書きました
var lst = (from mediaref in MediaRefs
join media in Medias
on mediaref.Int_MediaId equals media.Int_MediaId
join mediatype in MediaTypes
on media.Int_MediaTypeId equals mediatype.Int_MediaTypeId
where mediatype.Int_MediaTypeId_FK == 1
&& mediaref.Bit_IsActive == true
&& media.Int_OrganizationId == 2
&& media.Int_PostStatusId != 3
&& mediaref.Int_MediaRefId == mediaref.Media.
MediaRefs.FirstOrDefault(m => m.Int_MediaId == media.Int_MediaId
&& m.Bit_IsActive == true &&
(m.Nvcr_MediaTitle != null
&& m.Nvcr_MediaTitle != "")).Int_MediaRefId
orderby media.Int_MediaId descending
select new MediaViewModel {
MediaId = media.Int_MediaId,
Mediatitle = mediaref.Nvcr_MediaTitle ,
Mediapath = mediaref.Vcr_MediaPath,
Slug = mediaref.Vcr_Slug,
Active = mediaref.Bit_IsActive
});
その後、これに変更
var lst = (from mediaref in MediaRefs
join media in Medias
on mediaref.Int_MediaId equals media.Int_MediaId
join mediatype in MediaTypes on
media.Int_MediaTypeId equals mediatype.Int_MediaTypeId
where mediatype.Int_MediaTypeId_FK == 1
&& mediaref.Bit_IsActive == true
&& media.Int_OrganizationId == 2
&& media.Int_PostStatusId != 3
&& mediaref.Nvcr_MediaTitle != ""
group new {mediaref, Medias, MediaTypes} by new {
media.Int_MediaId,
mediaref.Vcr_MediaPath,
mediaref.Vcr_Slug,
mediaref.Bit_IsActive
} into g
orderby g.Key.Int_MediaId descending
select new MediaViewModel {
MediaId = Convert.ToInt64(g.Key.Int_MediaId),
Mediatitle = g.Min(p => p.mediaref.Nvcr_MediaTitle) == "" ?
g.Max(p => p.mediaref.Nvcr_MediaTitle) :
g.Min(p => p.mediaref.Nvcr_MediaTitle),
Mediapath = g.Key.Vcr_MediaPath,
Slug = g.Key.Vcr_Slug,
Active = g.Key.Bit_IsActive
});
このクエリは、もう少し効率的な方法で記述できますか? media
問題は、最初の行に参加したいということmediaref
ですmedia
. 助言がありますか。この最初のクエリで使用したものFirstOrDefault
と、2 番目のクエリで使用したものgroup by
です。