0

my method

parms.ValidationObject = new { validations = new Dictionary<string, object>() { { parms.JsonObjectName, modelValidations } } };
    return parms.ValidationObject;

where ValidationObject is Object Type

Later on I call this method

var modelValidations = new Dictionary<string, object>();
var validations = GetValidations(nextParms);
modelValidations = modelValidations.Concat(validations).ToDictionary(x => x.Key, x => x.Value);

but I get

cannot convert from 'object' to 'System.Collections.Generic.IEnumerable>'

Simple cast to Dictionary<string, object> does not work. What can I do?

1
  • 1
    1) ValidationObject is not a direct intance of object, as you say. It is an anonymous type. 2) What is the return type of GetValidations(nextParms) Commented Dec 3, 2016 at 21:59

2 Answers 2

1

You weren't clear, but when you say "my method" is:

parms.ValidationObject = new { validations = new Dictionary<string, object>() { { parms.JsonObjectName, modelValidations } } };
    return parms.ValidationObject;

I assume you mean that the code above is the implementation to the method, GetValidations. If so, then the problem is that the return type of your method is probably object since it can't be the real type of parms.ValidationObject, since that is an anonymous type which can't leak out of the method.

So I think your real problem is that you are trying to consume the validations property defined on that anonymous type from an external method. Really you should step back and stop doing this. If you want to consume an anonymous type outside of the method in which it is declared, it means you've made a mistake.

Instead either use a Tuple variant to return the values you want, use out parameters, define a new concrete type altogether, or just return the dictionary (parms.ValidationObject.validations) itself -- it's entirely unclear why you're using an anonymous type here since you only define one property. Under no circumstance should you try to interact with anonymous types in a static fashion outside of the method in which the type is declared.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to cast validations to System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> before you pass it to Concat since that is what it expects.

Make this change as well:

parms.ValidationObject = new Dictionary<string, object>() { parms.JsonObjectName, modelValidations };

2 Comments

How do you know what the type of validations is?
because the OP said his method at the top returns it. And then he is trying to concatenate it with another dictionary and the error says that too. Your comment was helpful too about it not being a direct instance of dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.