【发布时间】:2018-08-22 02:39:30
【问题描述】:
我正在使用存储库模式并具有以下实体类和 DTO 类。在我的服务中,我正在实现一个接口来获取所有类别(类型)GetAlbumsInAllCategories 中的专辑(专辑名称)。我只需要在函数GetAlbumsInAllCategories 中的linq lamda 中按这些列分组返回这些字段-abbumcategory.type、song.album、song.albumcover。下面 GetAlbumsInAllCategories 中的 linq lamda 表达式在下面一行的 Select 关键字处给出了这个错误。
return albums.ToList().Select(Mapper.Map<AlbumsByCategory, AlbumsByCategoryDTO>);
错误是因为我只从存储库类 AlbumRepository 和导航属性歌曲中选择了几列。而且我不想为少数选定的列创建新的 DTO 类,有没有办法在不为少数选定的列创建新的 DTO 而是使用现有的 AlbumsByCategory 和导航歌曲的情况下做到这一点?
T-SQL 翻译
select
ab.[type],s.Album,s.[AlbumCover]
from
[dbo].[AlbumsByCategory] ab
join [dbo].[Songs] s on s.Id=ab.SongId
where ab.Archived=0 and ab.ShowByAlbums=1
group by
ab.[type],s.Album,s.[AlbumCover]
专辑分类
public partial class AlbumsByCategory
{
public int Album_Song_Id { get; set; }
public string Type { get; set; }
public int SongId { get; set; }
public bool ShowByAlbums { get; set; }
public Nullable<System.DateTimeOffset> FromDate { get; set; }
public Nullable<System.DateTimeOffset> ToDate { get; set; }
public bool Archived { get; set; }
public virtual Song Song { get; set; }
}
歌曲
public partial class Song
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Song()
{
this.Favorites = new HashSet<Favorite>();
this.HitMiscSongs = new HashSet<HitMiscSong>();
this.PlayListSongs = new HashSet<PlayListSong>();
this.AlbumsByCategories = new HashSet<AlbumsByCategory>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Artist { get; set; }
public string Genre { get; set; }
public string AlbumCover { get; set; }
public string Album { get; set; }
public string ContentType { get; set; }
public string FilePath { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Favorite> Favorites { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<HitMiscSong> HitMiscSongs { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PlayListSong> PlayListSongs { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AlbumsByCategory> AlbumsByCategories { get; set; }
}
DTO 类
public class AlbumsByCategoryDTO
{
public int Album_Song_Id { get; set; }
public string Type { get; set; }
public int SongId { get; set; }
public bool ShowByAlbums { get; set; }
public Nullable<System.DateTimeOffset> FromDate { get; set; }
public Nullable<System.DateTimeOffset> ToDate { get; set; }
public bool Archived { get; set; }
public virtual SongDTO Song { get; set; }
}
public class SongEntity
{
public int Id { get; set; }
public string Title { get; set; }
public string Artist { get; set; }
public string Genre { get; set; }
public string AlbumCover { get; set; }
public string Album { get; set; }
public string ContentType { get; set; }
public string FilePath { get; set; }
public virtual ICollection<FavoriteEntity> Favorites { get; set; }
public virtual ICollection<PlayListSongEntity> PlayListSongs { get; set; }
}
GetAlbumsInAllCategories 中上述 TSQL 的 Linq Lamda
public class AlbumServices : IAlbumServices
{
private readonly UnitOfWork _unitOfWork;
/// <summary>
/// Public constructor.
/// </summary>
public AlbumServices(UnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IEnumerable<AlbumsByCategoryEntity> GetAlbumsInAllCategories()
{
var albums = _unitOfWork.AlbumRepository.GetAll()
.Where(y => y.Archived == false && y.ShowByAlbums == true)
.GroupBy(y => new { y.Type, y.Song.Album, y.Song.AlbumCover })
.ToList()
.SelectMany(x => x.Select(y => new { y.Type, y.Song.Album, y.Song.AlbumCover }));
if (albums.Any())
{
return albums.ToList().Select(Mapper.Map<AlbumsByCategory, AlbumsByCategoryDTO>);
}
return Enumerable.Empty<AlbumsByCategoryDTO>();
}
}
【问题讨论】:
标签: c# entity-framework linq linq-to-entities entity-framework-5