I'm wondering if is it possible to do a linq query like this:
Dictionary<(int,int), object)> entity = _context.Table.ToDictionary((i => i.IDOrigin, i.IDDestiny), o => o);
I know that it's possible with an unique key .ToDictionay(i => i.Key, o => o.Value);
but in this case this table has a double PK and I need both as keys.
Thank you.
CodePudding user response:
You just need to tweak your call a bit:
_context.Table.ToDictionary(i => (i.IDOrigin, i.IDDestiny), o => o)
All I've moved is the brackets: you still want i
as a regular lambda expression parameter, but you want the result of the lambda to be a tuple literal, which is the (i.IDOrigin, i.IDDestiny)
part.
Note that the o => o
part is unnecessary - if you don't specify a lambda expression for projecting from a source element to a value, ToDictionary
uses the source element as the value. So you can use:
_context.Table.ToDictionary(i => (i.IDOrigin, i.IDDestiny))