1

首先我写了这个查询

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,每行总是会带来 3 行media。有什么建议么。在第一个查询中我使用过FirstOrDefault,在第二个查询中我使用过group by.

4

1 回答 1

0

我想我会选择您查询的第一个版本,但会进行一些修改。有点像这样(我不会写下全部内容):

from m in Medias
where m.MediaType.Int_MediaTypeId_FK == 1
let firstmediaref = m.MediaRefs
                     .FirstOrDefault(ref => ref.Bit_IsActive 
                           && !string.IsNullOrEmpty(ref.Nvcr_MediaTitle))

MediaViewModel并用mand构建你的firstmediaref

于 2013-05-31T13:06:26.963 回答