How can I access the values ​​of foreign key related columns using LinqToSql?

I am trying to hook up LinqToSql to my data access layer and work in several problems, possibly due to my lack of experience with LinqToSql.

I have two tables, one called Project and one called Employee. The project has fields for OpenedBy and ClosedBy, which are external key references to the Employee table, which has fields for EmployeeId and Name.

When I get the project I would like it to fetch the EmployeeName for OpenedBy and ClosedBy. I would like to access them like this:

// assuming data is of type project

this.OpenedByName.Text = data.OpenedByName;
this.ClosedByName.Text = data.ClosedByName;

      

Is it also possible to set these values ​​whenever OpenedBy or ClosedBy changes? Is it possible? Sample code would be greatly appreciated!

Clarification

I would like to do this without using stored procedures.

0


source to share


1 answer


If you have 2 relationships coming from Employee table I think you will have 2 child properties project.Employee

and project.Employee1

in each Project object.

You can change the name of the association, just go to the properties of the relationship, select Child Property and change the name of each child Employee to be more descriptive.



You can name the child properties what you want, for example you could:

this.OpenedByName.Text = data.OpenedByEmployee.Name;
this.ClosedByName.Text = data.ClosedByEmployee.Name;

      

+1


source







All Articles