The given key 'EmptyProjectionMember' was not present in the dictionary
See original GitHub issueIn the following method, i got exception which i mentioned the title. But if i uncomment the following line it works.
// PreferenceIds = new List<Guid>()
In which cases this type of exception may occur, any clue?
public static Expression<Func<ReservationGuest, GuestDto>> GuestSelector
{
get
{
return g => new GuestDto
{
Id = g.Id,
ProfileId = g.ProfileId,
VipLevelId = g.ProfileId.HasValue ? g.Profile.VipLevelId : g.ProfileId,
VipLevelCode = g.ProfileId.HasValue
? g.Profile.VipLevel.Code
: (g.VipLevelId.HasValue ? g.VipLevel.Code : null),
LanguageId = g.ProfileId.HasValue ? g.Profile.Language.Id : g.LanguageId,
LanguageName = g.ProfileId.HasValue
? g.Profile.Language.Name
: (g.Language != null ? g.Language.Name : string.Empty),
BirthDate = g.ProfileId.HasValue ? g.Profile.BirthDate : null,
IsOwner = g.IsOwner,
// PreferenceIds = new List<Guid>(), // works fine
PreferenceIds = g.ProfileId != null && g.Profile != null // exception
? g.Profile.Preferences.Select(p => p.PreferenceId).ToList()
: g.Preferences.Select(p => p.PreferenceId).ToList(),
FirstName = g.ProfileId.HasValue ? g.Profile.FirstName : g.FirstName,
LastName = g.ProfileId.HasValue ? g.Profile.LastName : g.LastName,
MiddleName = g.ProfileId.HasValue ? g.Profile.MiddleName : g.MiddleName,
PhoneCode = g.ProfileId.HasValue
? g.Profile.Communications
.Where(c => c.Type == CommunicationType.Phone && c.IsPreferred)
.Select(y => y.PhoneCode).FirstOrDefault()
: g.PhoneCode,
PhoneNumber = g.ProfileId.HasValue
? g.Profile.Communications
.Where(c => c.Type == CommunicationType.Phone && c.IsPreferred)
.Select(c => c.Value).FirstOrDefault()
: g.PhoneNumber,
Email = g.ProfileId.HasValue
? g.Profile.Communications
.Where(c => c.Type == CommunicationType.Email && c.IsPreferred)
.Select(c => c.Value).FirstOrDefault()
: g.Email
};
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (7 by maintainers)
Top Results From Across the Web
c# - Why LINQ Sum() throws "System.Collections.Generic. ...
System.Collections.Generic.KeyNotFoundException: 'The given key 'EmptyProjectionMember' was not present in the dictionary.' var res = ( ...
Read more >"The given key was not present in the dictionary." Error
To fix the problem of "The provided key wasn't found in the dictionary," attempt verifying whether all essential keys are outlined within the ......
Read more >[Solved] The given key was not present in the dictionary.
Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. source error : Line 54: ...
Read more >Getting error: The given key was not present in the dictionary
Hi @ppr & @kumar.varun2 I'm getting error as the key is not present in the dictionary. The datatable is same.
Read more >C# KeyNotFoundException: Key Not Present in Dictionary
A solution. As always we want a quick way to fix the problem. We can use an if-statement with TryGetValue to avoid this...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@alexdresko this issue was closed two years ago; if you’re still seeing this error, can you please open a new issue?
Here’s the simplest possible repo, using the starting point: Getting Started - EF Core | Microsoft Learn
The following query throws the exception. Ignore the fact that this is a silly way to use EF. I’m just trying to narrow the problem down to its smallest repro, and this is what I came up with. It’s the 2nd Select that causes the exception to be thrown.