Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6922067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:21:31+00:00 2026-05-27T10:21:31+00:00

im working with a new ASP.NET MVC3 project and it seems like im missing

  • 0

im working with a new ASP.NET MVC3 project and it seems like im missing something in my LINQ skills.

Im formatting data for a Json “call” to be used my a jqGrid.

It works fine but now i want to add a related object by a Linq .Include() expression.
Think i better show with code.

var query = db.Products.Include("Category");

var jsonData = new
{
    total = 1,  // calc
    page = page,
    records = db.Products.Count(),
    rows = query.Select(x => new { x.Id, x.Name, x.PartNr })
        .ToList()
        .Select(x => new { 
            id = x.Id,
            cell = new string[] {
                x.Id.ToString(),
                x.Name.ToString(),
                x.PartNr.ToString(),
                //x.Category.Name.ToString() 
                //This does not work but object is there.
        }}).ToArray(),
    };

    return Json(jsonData, JsonRequestBehavior.AllowGet);

Problem area => //x.Category.Name.ToString()
The strange thing here is if i Break and watch the query (//x.Category.Name.ToString()) i can actually find the attached Category object but how, if possible, can i use it in my ano method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:21:32+00:00Added an answer on May 27, 2026 at 10:21 am

    The problem is that you are first selecting an anonymous object with the properties Id, Name and PartNr. Then you execute this query against the database (with ToList()) and then you do a new select on the list of anonymous objects and try to access a property that’s not in your anonymous object.

    You should include the category in your anonymous object so you can access it in the second select. Or you should select the final structure with the first select query so it will be executed against your database.

    This would work for example:

    rows = query.Select(x => new { x.Id, x.Name, x.PartNr, x.Category })
                        .ToList()
                        .Select(x => new
                        {
                            id = x.Id,
                            cell = new string[] {
                            x.Id.ToString(),
                            x.Name.ToString(),
                            x.PartNr.ToString(),
                            x.Category.Name.ToString()
                            }
                        }).ToArray()
    

    Or you simplify your query to only one and execute against the database:

     rows = query.Select(x => new
            {
                x.Id,
                cell = new string[]
                {
                    x.Id.ToString(),
                    x.Name.ToString(),
                    x.PartNr.ToString(),
                    x.Category.Name.ToString()
                }
            }).ToArray()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to ASP.NET MVC3 and trying desperately to just get something simple
I am working on a project which adopted ASP.NET MVC3(Razor) tech. Now, I have
I would like to build a new web application using ASP.NET MVC3 and MongoDB.
I am currently working on an ASP.NET MVC3 project in Visual Studio. I have
I working on asp.net mvc3 project. I am using autofac for DI. I have
Im working on porting some old ALP user accounts to a new ASP.Net solution,
I am new to C#.Working in ASP.NET 3.5 ( C# 3.0). What are the
I'm working on a new ASP.NET MVC / JQuery app which will need to
I am working on an existing project in ASP.NET witch is based in a
I am working on ASP.NET MVC3. I have a partial view RestaurantAdminNavigation.ascx which i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.