Alex Rivera | Logout

ToDictionary not working as expected

Asked 2010-11-22T15:45:58.990
12

Given the following code, I am having trouble returning a Dictionary.

[JsonProperty]
public virtual IDictionary<Product, int> JsonProducts
{
    get
    {
        return Products.ToDictionary<Product, int>(x => x.Key, v => v.Value);
    }
}

public virtual IDictionary<Product, int> Products { get; set; }

I get the following errors..

'System.Collections.Generic.IDictionary' does not contain a definition for 'ToDictionary' and the best extension method overload 'System.Linq.Enumerable.ToDictionary(System.Collections.Generic.IEnumerable, System.Func, System.Collections.Generic.IEqualityComparer)' has some invalid arguments

cannot convert from 'lambda expression' to 'System.Func'

cannot convert from 'lambda expression' to 'System.Collections.Generic.IEqualityComparer

There is nothing special about the Product class. it is simply defined as

class Product 
{
    public virtual int Id { get; set; }
    public virtual String Name { get; set; }
}
Edit
Report

1 Answer

0

If you're not actually cloning the Product instances, you can just do:

public virtual IDictionary<Product, int> JsonProducts
{
    get
    {
        return new Dictionary(Products);
    }
}
answered 2010-11-22T16:03:31.177

Your Answer