0

I checked similar questions such as:

How do I select a collection within a collection using LINQ?

How to return ICollection<object> from linq, when using select new object() {}

What I'm trying to do is return the list in Json format to be used in a Javascript GET request and display it on a cshtml page.

        var query = (from c in db.photos
                     orderby c.date_created descending
                     select new { id = c.id, name = c.name, image_url = c.image_url, description = c.description, photos_has_characters = c.photos_has_characters.FirstOrDefault() })
                     .Skip(pageIndex * pageSize)
                     .Take(pageSize);
        return Json(query.ToList(), JsonRequestBehavior.AllowGet);

However, when I go to the page nothing loads or shows an error I can pinpoint, so it has to be the photos_has_characters part in the LINQ.

How do I accomplish displaying those associated entities? Let me re-clarify. So I have a character's table and a photos tables with a many to many relationship table called photos_has_characters. I want to also select the character name and id from that photos_has_characters collection and return that information with the select new object. How do I do that?

If I'm missing something I will add it, thank you in advance.

EDIT:

In client-side on console I get this error: enter image description here

What the LINQ is returning for photos_has_characters parameter... Although it doesn't return an error, the parameter for new object of the photos_has_characters collection returns this:

photos_has_characters = {System.Data.Entity.DynamicProxies.photos_has_character_A0E9952F88D75712B3DACC51D4EA09329C0427E5A9FED3F11F4EE0EC2AE1B579}

That into Json to be read using Javascript results in nothing. How do I modify my LINQ to accomodate?

Royce
  • 189
  • 6
  • 18
  • What you're missing: When you debug your action, what does `query` contain? Is the debugger stopping in your action? On the client-side -- what does the network monitor show for the GET request? What is the status code? – Jasen Jul 15 '17 at 20:32
  • The status code I get is 500 Internal Server Error – Royce Jul 15 '17 at 20:38
  • 500 Internal Server Error is the catch-all server-side error. You can enable IIS to return the error details in addition to running the debugger to break in the action method and to trap the error there. – Jasen Jul 15 '17 at 20:41
  • If you are using Entity Framework you can also see the actual sql query https://msdn.microsoft.com/en-us/library/dn469464(v=vs.113).aspx. Or use the SQL Server profiler to see what the database is returning. – Jasen Jul 15 '17 at 20:46
  • Although it doesn't return an error, the parameter for new object of the photos_has_characters collection returns this: photos_has_characters = {System.Data.Entity.DynamicProxies.photos_has_character_A0E9952F88D75712B3DACC51D4EA09329C0427E5A9FED3F11F4EE0EC2AE1B579} – Royce Jul 15 '17 at 20:47
  • What is the type for `photos_has_character` and does your JSON serializer know how to convert it? – Jasen Jul 15 '17 at 20:49
  • the type for photos_has_character is an ICollection type of (photo id, character id, date created), in Javascript I make an ajax call for data type json to read and display the data. – Royce Jul 15 '17 at 20:53
  • Define a class instead of using an anonymous object for your select projection. Then try to serialize it `Newtonsoft.Json.JsonConvert.SerializeObject()`. Figure that out then plug it into your action. – Jasen Jul 15 '17 at 20:53
  • Thanks Jasen for the debugging help. I think I got my desired solution. I modified to include another select statement and added new parameter in Javascript and it loads images with the characters name from other table now. Thanks again! – Royce Jul 15 '17 at 21:14

0 Answers0