0

I have a problem I cant make a Linq query which intersect my dictionary keys with list, and back to the origin dictionray without this which are gone.

var gg = request
    .Number_Name
    .Keys
    .Intersect(_context.Users.Select(x => x.Phone))
    .ToDictionary(t => t);

I tried something like this, but it doesnt work.

smolchanovsky
  • 1,775
  • 2
  • 15
  • 29
Redduke
  • 27
  • 6
  • does this provide you with an answer: https://stackoverflow.com/questions/10685142/c-sharp-dictionaries-intersect –  Jan 23 '20 at 13:04

1 Answers1

0

try this code :

1 - Get keys

var keys = request
    .Number_Name
    .Keys
    .Intersect(_context.Users.Select(x => x.Phone));

2 - Get new dictionary

var newDic = request
    .Number_Name
    .Where(k => keys.Contains(k.Key))
    .ToDictionary(k => k.Key, v => v.Value);

Or

var newDic = request
    .Number_Name
    .Keys
    .Intersect(_context.Users.Select(x => x.Phone))
    .ToDictionary(k => k, v => request.Number_Name[v]);
Mohammed Sajid
  • 4,778
  • 2
  • 15
  • 20