1

fromI have this linq query to build a json (of string and bool?) from an IQueryable :

var ret = from c in results select new { country = c.EN, schengen = c.Schengen };

I would like to append new items to it manually : (pseudo code)

ret = ret /* add this -> */ { country = "some new country", schengen = true }

I've tried to do that :

//End up with cannot convert from AnonymousType#2 to AnonymousType#1
var listRet = ret.ToList();
listRet.Add(new { country ="", schengen = true });

As it is an anonymous type that I build I cannot find a way to add it. I always end up with conversion error

2
  • 1
    What is the type of c.Shengen in your example?
    – Sean
    Nov 15, 2012 at 13:43
  • it's bool I specify it when I explain that I build json
    – Arno 2501
    Nov 15, 2012 at 13:44

3 Answers 3

2

Anonymous types are still statically typed and C# compiler generates a separate class for each anonymous type definition occurrence. You need a named class in this case. It's a good practice for data transfer objects anyway. I use JSON DTOs like this, using data annotations and DataContractJsonSerializer.

[DataContract]
public class CountryInfo
{
    [DataMember(Name = "country")]
    public string Country { get; set; }

    [DataMember(Name = "schengen", EmitDefaultValue = false)
    public bool? Schengen { get; set; }
}

This way I have a well-documented JSON "protocol", which also uses C# naming conventions before serialization, but JS naming conventions after.

1
  • 1
    You're welcome. Also as Eric Johansson mentioned, you can nest this class inside your current class (I guess it's some sort of web service) and make it private - it won't "pollute" the rest of the assembly/application then. Nov 15, 2012 at 13:53
2

Well it's obvious. Generics are invariant. So another type won't fit. You can use List<object> or a named class to store the values.

3
  • Upvoted, but I would add that compiler produce different Anonymous classes even if fields match.
    – Andrey
    Nov 15, 2012 at 13:39
  • @Andrey: I thought it did the opposite when everything matches. Or maybe just under certain circumstances.
    – leppie
    Nov 15, 2012 at 13:40
  • Well it's too obvious to add this, because he has already posted the error. ;o) But you're right.
    – DHN
    Nov 15, 2012 at 13:41
1

If you want to change the result manually, you need to cast it into a static type instead of a dynamic one. Create a class that has a string country and bool schengen(You can even create the class within your current class to hide it from the rest of the application, if that's applicable), cast your linq results into that and then just add to it.

2
  • 1
    Well I guess you mean a static type. ;o) A static class is something totally different.
    – DHN
    Nov 15, 2012 at 13:43
  • Yeah, I meant a static type, not class. Sorry. -- Updated Nov 15, 2012 at 13:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.