I use Mapster Mapper to project from domain models to DTOs. That works perfectly, but now I had to use a GroupBy/Select and that breaks my code:
public class Document
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string? LanguageId { get; set; }
public string? Slug { get; set; }
public string? Name { get; set; }
}
public class DocumentDto
{
public Guid Id { get; set; }
public string? LanguageId { get; set; }
public string? Slug { get; set; }
public string? Name { get; set; }
}
public async Task<IList<T>> GetDocuments<T>(string? slug, string? languageId)
{
var iq = _dataContext.Documents
.Where(x => slug == null || (slug != null && x.Slug == slug))
if (languageId != null)
{
iq = iq.GroupBy(x => x.Slug)
.Select(x => x
.OrderByDescending(y => y.LanguageId == languageId)
.ThenByDescending(y => y.LanguageId == "da")
.ThenBy(y => y.Name)
.First());
}
else
{
iq = iq.OrderBy(x => x.Name).ThenBy(x => x.LanguageId);
}
return await iq.AsNoTracking().ProjectToType<T>().ToListAsync();
}
var documents = await GetDocuments<DocumentDto>(null, "en");
An unhandled exception has occurred while executing the request. System.Collections.Generic.KeyNotFoundException: The given key 'EmptyProjectionMember' was not present in the dictionary.
I have no idea how to handle this, what it means and where to look. Please help.