我隊 S的編碼器小號項目 S的任務 S的。如何根據兒童孩子的人數排序EF父母?
如何有效地排序隊 01用任務 S IN實體框架的使用方法的語法和代碼優先的方法號(可能沒有加載兒童兒童的整個集合內容,我需要那種只)?
public class MyAppContext : DbContext
{
public DbSet<CTeam> Teams { get; set; }
// ...etc
}
[Table("Team")]
public class CTeam
{
public int ID { get; set; }
[MaxLength(128)]
public string Title { get; set; }
[Display(Name = "Coders")]
public virtual ICollection<CCoder> Coders { get; set; }
}
[Table("Coder")]
public class CCoder
{
public int ID { get; set; }
[MaxLength(128)]
public string Name { get; set; }
public virtual ICollection<CProject> Projects { get; set; }
public int TeamID { get; set; }
public virtual CTeam Team { get; set; }
}
// CProject and CTask similarly
或者,如果你可以給一個暗示原始的SQL應該怎麼看起來像這個,這將有助於太。謝謝!
UPDATE
與關係Builder -> NewBuildingObject -> NewBuildingHouse -> NewBuildingLayout
對於觀衆的舒適,特奧多爾·伊萬諾夫的回答一個項目:
context.Teams
.OrderBy(x => x.Coders
.SelectMany(y => y.Projects)
.SelectMany(y => y.Tasks)
.Count());
做出EF生成驗證碼:
SELECT
[Project1].[ID] AS [ID],
[Project1].[Title] AS [Title]
FROM (SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Title] AS [Title],
(SELECT
COUNT(1) AS [A1]
FROM [dbo].[NewBuildingObject] AS [Extent2]
INNER JOIN [dbo].[NewBuildingHouse] AS [Extent3] ON [Extent2].[ID] = [Extent3].[ObjectID]
INNER JOIN [dbo].[NewBuildingLayout] AS [Extent4] ON [Extent3].[ID] = [Extent4].[HouseID]
WHERE [Extent1].[ID] = [Extent2].[BuilderID]) AS [C1]
FROM [dbo].[Builder] AS [Extent1]
) AS [Project1]
ORDER BY [Project1].[C1] ASC
和雅各布·馬薩德的回答是:
var teams = context.Teams
.OrderBy(team =>
team.Coders.SelectMany(coder =>
coder.Projects.SelectMany(project => project.Tasks)).Count());
使這個代碼:
SELECT
[Project1].[ID] AS [ID],
[Project1].[Title] AS [Title]
FROM (SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Title] AS [Title],
(SELECT
COUNT(1) AS [A1]
FROM [dbo].[NewBuildingObject] AS [Extent2]
INNER JOIN (SELECT [Extent3].[ObjectID] AS [ObjectID]
FROM [dbo].[NewBuildingHouse] AS [Extent3]
INNER JOIN [dbo].[NewBuildingLayout] AS [Extent4] ON [Extent3].[ID] = [Extent4].[HouseID]) AS [Join1] ON [Extent2].[ID] = [Join1].[ObjectID]
WHERE [Extent1].[ID] = [Extent2].[BuilderID]) AS [C1]
FROM [dbo].[Builder] AS [Extent1]
) AS [Project1]
ORDER BY [Project1].[C1] ASC
謝謝!您的解決方案運行良好。我最早接受這個答案。 – Roman