How to return LLBLGenPro Entities to JSON

08.07.2009 1

I was recently searching for a way to convert LLBLGenPro Entities to JSON when I ran across Josh’s approach. I like the approach but thought there must be an easier way. This approach requires LLBLGenPro 2.6 or above and .NET 3.0.

Here’s an example


            LinqMetaData metaData = new LinqMetaData();

            var q = from customer in metaData.Customer
                    where customer.Id == customerId
                    select new { id = customer.Id, name = p.FullName };

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(q);

Here’s another example with a 1:m return:

            LinqMetaData metaData = new LinqMetaData();

            var q = from meeting in metaData.ReviewMeeting
                    select new
                    {
                        CreatedBy = meeting.CreatedBy,
                        ReviewProvider = from providers in meeting.ReviewProvider
                                         select new { ProviderName = providers.FullName }
                    };

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(q);

Nice thing about using the new linq features in LLBLGenPro is you’re only bringing back the data you care about. Yes, you could always ignore fields, but isn’t it easier to tell it what you want instead of what you don’t want?

Thoughts? I’d love to hear them.

Responses to “How to return LLBLGenPro Entities to JSON”

Leave a reply