Join Dictionary<int, string> and List<MyClass> error (type arguments for method cannot be inferred from the usage)

StackOverflow https://stackoverflow.com/questions/15742647

Question

I´m trying to join a Dictionary<int, string> and List<MyClass> but it throws an error

"type arguments for method cannot be inferred from the usage".

But it seems to me all the arguments are perfectly defined...

class Row
{
        public string name { get; set; }
        public string[] data { get; set; }
}

Dictionary<int, string> devices = new Dictionary<int,string>();
List<Row> rows = new List<Row>();

rows = rows.Join(devices, row => row.data[0], device => device.Key, (row, device) => { row.data[1] = device.Value; return row; }).ToList();

row is a Row, device is a pair<int,string>, device.Key is an int, and device.Value is a k. What is the problem? I know it must be a silly thing, but I´m stuck with this error.

Was it helpful?

Solution

row.data[0] is a string, but device.Key is an integer, so the key types don't match.

OTHER TIPS

You're getting this error because device => device.Key is an int where as row.data[0] is a string. They are both of the generic type TKey and need to be consistent. It can be fixed by calling ToString() on device.Key.

rows = rows.Join(devices, row => row.data[0], device => device.Key.ToString(), (row, device) => { row.data[1] = device.Value; return row; }).ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top