0

In function below I am trying to return multiple attributes form multiple tables.

Parent table > Child table

AspNetUser > Ad

Ad > MobileAd

public async Task<IHttpActionResult> GetAd(int id)
{
    Ad add = await db.Ads.FindAsync(id);
    if (add == null)
    {
        return NotFound();
    }
    
    var ret = (from ad in db.Ads
               where ad.Id.Equals(id)
               orderby ad.time
               select new
               {
                   title = ad.title,
                   postedById = ad.AspNetUser.Id,
                   postedByName = ad.AspNetUser.UserName,
                   description = ad.description,
                   mobilead = ad.MobileAds.FirstOrDefault(x => x.adId == ad.Id),
               });
    return Ok(ret);
}

In ajax request to fetch data I got Internal Server Error and below is the figure of debugging.

Debugging picture

Why it is fetching objects instead of data?

Updated:

enter image description here

When I click on refresh icon in Results View during debugging. Its shows data! but not returning it.

2
  • Could you include the exception? Commented Aug 11, 2015 at 9:13
  • I didn't get exception. Internal Server Error is the error in console window Commented Aug 11, 2015 at 9:15

1 Answer 1

1

You may consider using

 return Ok(ret.ToArray());

instead of

 return Ok(ret);

otherwise linq query above will not be evaluated.

Sign up to request clarification or add additional context in comments.

16 Comments

1. Have you see image attached in question. You can see its not returning values(data). 2.I am returning single record so there is no need to use ToArray 3.I used used ToArray but no success. Request always go to `ajax().fail() function.
1. Of course I have seen the attached image, but the debug line over the variable "ret" was not expanded, so could not get enough info. 2. in order to enumarate and execute a linq query, ToArray, ToList or a loop statement must be used; otherwise it only stands as a statement only.
On expand its shows non public members and results view which I think was of no use.
there must be a refresh icon near to "results view". By clicking on it Linq query is executed and results are shown below it.
Can you try .FirstOrDefault() at the end of the Linq?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.