Tuesday, 15 May 2012

c# - Access fields tables relations with LINQ -



c# - Access fields tables relations with LINQ -

i have 2 tables: post , user.

the user has id , name... fields. , post has userid , title... fields.

im using linq, , want able write this:

var post = dc.posts.firstordefault(); var user = post.user;

then want able do: post.user.name ...

help please..

assume using entity framework. create sure have navigation properties on entities, , lazy loading enabled. code should work then:

var post = dc.posts.firstordefault(); if (post != null) name = post.user.name;

also can eager loading of user entity when loading post:

var post = dc.posts.include(p => p.user).firstordefault(); if (post != null) name = post.user.name;

c# database linq relation

No comments:

Post a Comment