Skip to content

Instantly share code, notes, and snippets.

@kristofclaes
Created November 2, 2011 08:30
Show Gist options
  • Save kristofclaes/1333181 to your computer and use it in GitHub Desktop.
Save kristofclaes/1333181 to your computer and use it in GitHub Desktop.
Doing two joins for one table?
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; }
}
var results = from c in context.Children
select new
{
c.ChildId,
c.CreatedOn,
Parent = new
{
c.Parent.Name,
c.Parent.Description,
c.Parent.ExtraInfo
}
};
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