MVC view, Linq query to bring back foreign key

I want to display a list of customers and customer status in an unordered list in an MVC view.

I have customer table which has a status id linked to status table

Customer                               Status
ID                                           StatusID
Name                                     StatusName
StatusID

So I want to display

I'd.              Name.                   Status
1               John Doe.             Pending


ToString1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John ClaesSenior .Net Consultant & Technical AnalistCommented:
the Folowing are the correct ways to demand your requested display values.

Customer.ID
Customer.Name
Customer.Status.StatusName

Why are u using Linq for this?
ToString1Author Commented:
I am using linq to sql with sql server database.

It is just the query I need but for some reason if I put Customer.Status.StatusName   I get object not set error.  

What would the linq query be?
John ClaesSenior .Net Consultant & Technical AnalistCommented:
ha ok


Can you give me your Query?

Just after the Customers you should add .Expand("Status") ;-)
This will ensure that you can use the Status Attribute as it is Loaded
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

ToString1Author Commented:
OK thanks

I'm not allowed to post any code so could you please give an example code snippet?
John ClaesSenior .Net Consultant & Technical AnalistCommented:

In your code You should somewhere use something like
context.Customers

var customers = from c in repository.Customers  select c;
or
var customers = repository.Customers


Now we're going to Expand the Status to ensure loading

var customers = from c in repository.Customers.Expand("Status")   select c;
or
var customers = repository.Customers.Expand("Status")


ToString1Author Commented:
Hi

If I try the expand keyword it is not recognized

'System.Data.Linq.Table<test.Models.customer>' does not contain a definition for 'Expand' and no extension method 'Expand' accepting a first argument of type 'System.Data.Linq.Table<test.Models.customer>' could be found (are you missing a using directive or an assembly reference?)      
John ClaesSenior .Net Consultant & Technical AnalistCommented:
using System.Linq;

;)
ToString1Author Commented:
Yes I am using system.linq

I can write queries fine but it is does not recognize the Expand keyword.  

I can't see any documentation on it either
Fernando SotoRetiredCommented:
Hi ToString1;

The Expand method if I am not mistaken is part of LinqKit and not from the standard Linq implementation.

In the database do you define a relationship between Customer and the Status tables? From the error message it seems like you do not have one defined.

Fernendo
kumar754Commented:
use, the DataLoadOptions() in your case...
this is used to load related and linked tables..

refer this url: http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.loadwith.aspx

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.