Created
November 2, 2011 08:30
-
-
Save kristofclaes/1333181 to your computer and use it in GitHub Desktop.
Doing two joins for one table?
This file contains hidden or 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
public class Parent | |
{ | |
public int ParentId { get; set; } | |
public int Name { get; set; } | |
public string Description { get; set; } | |
public string ExtraInfo { get; set; } | |
public string AnotherExtraInfo { get; set; } | |
public string AndAnotherExtraInfo { get; set; } | |
public virtual ICollection<Child> Children { get; set; } | |
public Parent() | |
{ | |
Children = new List<Child>(); | |
} | |
} | |
public class Child | |
{ | |
public int ChildId { get; set; } | |
public int ParentId { get; set; } | |
public DateTime CreatedOn { get; set; } | |
public virtual Parent Parent { get; set; } | |
} |
This file contains hidden or 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
var results = from c in context.Children | |
select new | |
{ | |
c.ChildId, | |
c.CreatedOn, | |
Parent = new | |
{ | |
c.Parent.Name, | |
c.Parent.Description, | |
c.Parent.ExtraInfo | |
} | |
}; |
This file contains hidden or 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
SELECT [Extent1].[ChildId] AS [ChildId], | |
[Extent1].[CreatedOn] AS [CreatedOn], | |
[Extent2].[Name] AS [Name], | |
[Extent3].[Description] AS [Description], | |
[Extent3].[ExtraInfo] AS [ExtraInfo] | |
FROM [dbo].[Children] AS [Extent1] | |
INNER JOIN [dbo].[Parent] AS [Extent2] | |
ON [Extent1].[ParentId] = [Extent2].[ParentId] | |
LEFT OUTER JOIN [dbo].[Parent] AS [Extent3] | |
ON [Extent1].[ParentId] = [Extent3].[ParentId] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment