Created
September 20, 2013 16:21
-
-
Save mikecole/6640103 to your computer and use it in GitHub Desktop.
Eager Loading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Uses string notation - lots of typos! | |
//var posts = context.Posts | |
// .Include("Author") | |
// .ToArray(); | |
//Uses lambda notation - Intellisense and easy refactoring = love | |
//Note: include System.Data.Entity to use. | |
var posts = context.Posts | |
.Include(p => p.Author) | |
.ToArray(); | |
var model = (from post in posts | |
select new PostsViewModel | |
{ | |
Title = post.Title, | |
Url = post.Url, | |
AuthorName = post.Author.Name, | |
AuthorTwitterHandle = post.Author.TwitterHandle | |
}).ToArray(); | |
public class Post : EntityBase | |
{ | |
public string Title { get; set; } | |
public string Url { get; set; } | |
public virtual Author Author { get; set; } | |
} | |
public class Author : EntityBase | |
{ | |
public Author() | |
{ | |
Posts = new Collection<Post>(); | |
} | |
public string Name { get; set; } | |
public string TwitterHandle { get; set; } | |
public virtual ICollection<Post> Posts { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment