Chit Khine
Chit Khine

Reputation: 850

Cannot implicitly conver type System.Collection.Generic.Dictionary<string,IList<string>> to System.Collection.Generic.Dictionary<string,IList<string>>

I have getting this error when I try to insert the value where I get from using the method item.ToDictionary() b to the Dictionary<string,IList<string>> a that I already initialized it.

Cannot implicitly conver type System.Collection.Generic.Dictionary<string,List<string>> to System.Collection.Generic.Dictionary<string,IList<string>>
var b = item.ToDictionary(); 

I used this function to create b and try to insert into a.

 public class Example {
    private Dictionary<string, IList<string>> a;
    private string c;
    private IList<string> d;

    public Example()
    {
        d = new List<string>();
        a = new Dictionary<string, IList<string>>();
    }
    public void getDictionary()
    {
        var b = c.ToDictionary(e=>c, f=>d.ToList());
        a = b;
    }
}

Upvotes: 0

Views: 190

Answers (2)

lenkan
lenkan

Reputation: 4415

The problem is that d.ToList() returns a List<string>, so b will be of type Dictionary<string, List<string>>, which is different from Dictionary<string, IList<string>>. You can cast it to a IList<string> to solve the problem:

public void getDictionary()
{
    var b = c.ToDictionary(e => c, f => (IList<string>) d.ToList());
    a = b;
}

Upvotes: 0

user6996876
user6996876

Reputation:

Define b as

  var b = c.ToDictionary(e=>c, f=>(IList<string>)d);

Upvotes: 1

Related Questions