Serialize Linq Results directly to JSON
Asked Answered
S

3

7

I'm developing a WebService that excecute linq to sql db and put the results into a VAR variable. Then I wanna serialize the result inside VAR to json format using javascript serializer (c#). Something like this:

var sb= from p in ent.people .........
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new       System.Runtime.Serialization.Json.DataContractJsonSerializer(sb.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, sb);
string json = System.Text.Encoding.Default.GetString(ms.ToArray());

BUT I GET AN ERROR RESPONSE LIKE THIS:

Type      'System.Data.Objects.ObjectQuery`1[<>f__AnonymousType2d`5[System.String,System.Nu llable`1[System.Int32],System.Nullable`1[System.Int32],System.Int32,System.String]]' cannot be serialized. 

Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

HOW CAN I SERIALIZE LINQ RESULTS DIRECTLY TO JSON?? Thanks a lot for all answers! Enrico

Sofko answered 18/10, 2011 at 17:40 Comment(0)
D
6

DataContractJsonSerializer doesn't support anonymous objects. If you want to serialize anonymous objects you could use the JavaScriptSerializer class:

var sb = from p in ent.people .........
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(sb);
Deliciadelicious answered 18/10, 2011 at 17:46 Comment(1)
I distinctly remember a team I used to work with solving this problem using the DataContract serializers. We had web-layer code that called back to a repository on a different server through a WCF service, and while there were some restrictions on what the query could be and do, it worked.Phono
M
5

you can use Newtonsoft.JSON for that

heres's the syntax

var sb = from p in ent.people .........
string json = JsonConvert.SerializeObject(sb);
Mayhap answered 6/7, 2018 at 17:53 Comment(0)
P
0

At my last job, we saw this behavior and it took a bit of doing to get around it.

First, you want the IQ Toolkit, available from CodePlex for free. In its libraries is a "PartialEvaluator" that can reduce the complexity of many expression trees by finding nodes that always evaluate to simpler nodes, and by replacing references to "external closures" with constants. You'll want to run your IQueryable through this before trying to serialize it.

Then, in order to use the JSON DataContract serializer, you must set up the class you wish to serialize as a DataContract. There are tutorials for doing this all over the place; basically you just decorate the class, any contained classes, and the members that you want to serialize with attributes.

Once you have those two things in place, your object and its IQueryable member should be serializable to JSON using the DataContractJsonSerializer.

Phono answered 18/10, 2011 at 17:51 Comment(2)
So, if i understand i must download iq toolkit (i suppose is a dll) and then i pass my linq results to its calling partial evaluator method and after i can serialize the manipolated result using dataContractJsonSerializer?Sofko
One more step; you have to properly set up the class you want to serialize as a DataContract by decorating the class and its members with attributes.Phono

© 2022 - 2024 — McMap. All rights reserved.