DaveN
DaveN

Reputation: 918

LINQ ToDictionary System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.String'.'

Going bonkers here, but have a LINQ query that i want to convert to a Dictionary object, but getting a "System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.String'.'" error when doing so, so I'm stumped. It only happens if I just want to use the Key and use a Product class as the value.

ProductsDataContext context = new ProductsDataContext();

Dictionary<int, Product> result = context.Products.Where(x => x.ProductName.StartsWith("N")).ToDictionary(x => x.Prod_ID); 

The Prod_ID is an integer in the class and in the database. Nowhere should be converted to a string, so I'm stumped as to this error message.

If I write it something like this:

Dictionary<int, string> result = context.Products.Where(x => x.ProductName.StartsWith("N")).ToDictionary(x => x.Prod_ID, x=> x.ProductName)

It works fine and there is no data conversion error message (the Key is an integer as specified). I hadn't used this method before (w/LINQ to SQL), so I'm thinking that something is missing. (It works on non-SQL stuff)

Tried the same code on a different table and have gotten results (no errors). Looks like the problem was with nulls in the Product table (varchar() nulls - so the error message is completely off - a bug, perhaps?)

    StylesDataContext context = new StylesDataContext();

    Dictionary<int,Style> results = context.Styles.Select(x => x).Where(x => x.style_name.EndsWith("d")).ToDictionary(x => x.style_id);
    foreach (KeyValuePair<int, Style> r in results)
    {
        Console.WriteLine("id: " + r.Key.ToString()  + "\tName: " + r.Value.style_name + "\tGUID: " + r.Value.style_bvin  );
    }

Upvotes: 0

Views: 1072

Answers (2)

DaveN
DaveN

Reputation: 918

The answer to this particular question is that, somehow, Visual Studio 2017 corrupted a portion of the auto-generated code (I made no changes what-so-ever) for the LINQ-to-SQL classes. Deleting these classes and adding them back solved the problem regarding the error message for this query.

My presumption that the data was corrupt or nulls were present in the table causing the problem was incorrect.

I ran a harddrive scan to see if data was corrupted via my PC, but it came back clean.

If this happens again, I will be sure to run a full file-by-file comparison to see what portion was actually corrupted and post it here and to the MS Dev team. If this is a bug, it's a terrible, time-wasting, one.

Upvotes: 0

Dweeberly
Dweeberly

Reputation: 4777

Try the following code:

ProductsDataContext context = new ProductsDataContext();
var r1 = context.Products.Where(x => x.ProductName.StartsWith("N"));
var r2 = r1.ToDictionary(x => x.Prod_ID);

Does the error occur for r1 or r2? If you are using VS hold over the variable name to see what type is being used for each var statement. It would seem possible that the Enumerable for Products may be generating the error.

Upvotes: 1

Related Questions