Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have got method like this in my controller ...

in my controller i am receiving an id and and engine values as parameters and thn i am using these to filter the data. For some reason the turbo variable is always null even tho there is data in my database. I'm not sure to why this is .. could i possibly be missing anything?:?


C#
public ActionResult Vehicle( int? id, decimal? engine)
        {



            var turbo =
                _catalogue.Data.Where(x => x.manufacturerId == id && x.EngineLitreCapacity == engine)
                    .Select(x => x.Turbo).FirstOrDefault();
                



            var model =
                _catalogue.Data.FirstOrDefault(x => x.Turbo == turbo);
           
            return PartialView("_Vehicle", model);

          

    
        }


Thank you
Posted
Updated 6-Nov-15 3:25am
v3

FIRST, yes, THIS ONE IS FIRST...

Put a breakpoint on the first line in that method. Then run the code. Your code will stop on the breakpoint. Click in the Visual Studio window and hover the mouse on top of the variable names id and engine. It'll tell you what the values of those variables are. Make sure those values are what you expect. If not, you're digging around for a cause in the wrong place.

If those values ARE what you expect, then your WHERE clause in the LINQ query doesn't do what you think it does.
 
Share this answer
 
If _turbo is null, that means there is no record in the table where both the manufacturerId is equal to the id parameter and the EngineLitreCapacity is equal to the engine parameter.
 
Share this answer
 
First convert your LINQ query to SQL and run it from SSMS, see if you can actually get the data for the given conditions. Or do the vice-versa.

This way you can double check your result.

-KR
 
Share this answer
 
Comments
Dave Kreskowiak 6-Nov-15 10:10am    
Not first. First thing is to make absolutely sure the values coming into the method are what is expected. If they are correct THEN you can go spelunking around the query and the database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900