使用Automapper MapFrom通过WHERE过滤获取Count()

时间:2015-02-13 17:17:24

标签: automapper

鉴于此实体:

public class Album
{
public string Name { get; set; }
public string Artist { get; set; }
public virtual AlbumCollections AlbumCollections { get; set; }
}

与此DTO一起

public class AlbumDto
{
public string Name { get; set; }
public string Artist { get; set; }
public int AlbumCount { get; set; }
}

如何返回姓名和艺术家匹配的专辑数量(即重复专辑的数量)?

我的第一次尝试:

Mapper.CreateMap<Album, AlbumDto>()
                .ForMember(dest => dest.AlbumCount, opt => opt.MapFrom(so => so.AlbumCollections.Album.Where(x => x.Name == x.Name && x.Artist == x.Artist).Count()) );

...返回结果:

{
name: "My Album",
artist: "My Artist",
albumCount: "3"
},
{
name: "My Album",
artist: "My Artist",
albumCount: "3"
},
{
name: "My Different Album",
artist: "My Different Artist",
albumCount: "3"
}

计数应为&#34; 2&#34;在前两项,&#34; 1&#34;在最后一项。如何正确设置WHERE过滤器?

0 个答案:

没有答案