The given key 'EmptyProjectionMember' was not present in the dictionary

See original GitHub issue

In 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:closed
  • Created 4 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, May 16, 2023

@alexdresko this issue was closed two years ago; if you’re still seeing this error, can you please open a new issue?

1reaction
alexdreskocommented, May 15, 2023

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.

var blogs = await _bloggingContext.Blogs
    .GroupBy(p => p.BlogId)
    .Select(p => p.First())
    .Select(p => new { S = p.BlogId })
    .ToListAsync();
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found