Entity Framework 将多个模型投影到单个 DTO
Entity Framework Projecting Multiple Models to Single DTO
我想问一下如何将多个模型对象映射到一个DTO。
我有以下EF模型如下:
Bracket.cs
namespace MyApp.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Bracket")]
public partial class Bracket
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Bracket()
{
Teams = new HashSet<Team>();
}
public int BracketId { get; set; }
[Required]
[StringLength(50)]
public string BracketName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Team> Teams { get; set; }
}
}
Team.cs
namespace MyApp.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Team")]
public partial class Team
{
public int TeamId { get; set; }
[Required]
[StringLength(50)]
public string TeamName { get; set; }
public int BracketId { get; set; }
public virtual Bracket Bracket { get; set; }
}
}
然后我创建了 DTO 对象来表示它们。
namespace MyApp.Models
{
public class BracketDto
{
public int BracketId { get; set; }
public string BracketName { get; set; }
public List<TeamDto> Teams { get; set; }
}
public class TeamDto
{
public int TeamId { get; set; }
public string TeamName { get; set; }
public int BracketId { get; set; }
}
}
在我的 class 之一中,我用来查询数据库并投影结果数据,这就是我正在做的事情:
public List<BracketDto> GetBrackets()
{
var brackets = (from a in this.Context.Brackets
join b in this.Context.Teams on a.BracketId equals b.BracketId
select new BracketDto()
{
BracketId = a.BracketId,
BracketName = a.BracketName
}).ToList<BracketDto>();
var teams = (from a in this.Context.Teams
select new TeamDto()
{
TeamId = a.TeamId,
TeamName = a.TeamName,
BracketId = a.BracketId
}).ToList<TeamDto>();
foreach(BracketDto bracket in brackets)
{
var tl = teams.Where(a => a.BracketId == bracket.BracketId).ToList();
bracket.Teams = tl;
}
var xtra = (from a in this.Context.Brackets
join b in this.Context.Teams on a.BracketId equals b.BracketId
select new BracketDto()
{
BracketId = a.BracketId,
BracketName = a.BracketName
}).ToList<BracketDto>();
return brackets;
}
我在哪里获得结果:
[
{
"BracketId": 1,
"BracketName": "Eastern Conference",
"Teams": [
{
"TeamId": 1,
"TeamName": "Indiana Pacers",
"BracketId": 1
},
{
"TeamId": 3,
"TeamName": "Cleveland Cavaliers",
"BracketId": 1
}
]
},
{
"BracketId": 2,
"BracketName": "Western Conference",
"Teams": [
{
"TeamId": 2,
"TeamName": "Los Angeles Lakers",
"BracketId": 2
},
{
"TeamId": 4,
"TeamName": "Los Angeles Clippers",
"BracketId": 2
}
]
}
]
我知道可以有更有效的方法来做到这一点。但我不太确定如何在我的 LINQ 查询上执行此操作。
寻求意见和建议。谢谢!
所有的关系都准备好了,你只需要好好利用它们。
public List<BracketDto> GetBrackets()
{
var brackets = this.Context.Brackets.Select(a => {
return new BracketDto(){
BracketId = a.BracketId,
BracketName = a.BracketName,
Teams = a.Teams.Select(b => {
return new TeamDto() {
TeamId = b.TeamId,
TeamName = b.TeamName,
BracketId = b.BracketId
};
}).ToList()
};
}).ToList();
return brackets;
}
我想问一下如何将多个模型对象映射到一个DTO。
我有以下EF模型如下:
Bracket.cs
namespace MyApp.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Bracket")]
public partial class Bracket
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Bracket()
{
Teams = new HashSet<Team>();
}
public int BracketId { get; set; }
[Required]
[StringLength(50)]
public string BracketName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Team> Teams { get; set; }
}
}
Team.cs
namespace MyApp.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Team")]
public partial class Team
{
public int TeamId { get; set; }
[Required]
[StringLength(50)]
public string TeamName { get; set; }
public int BracketId { get; set; }
public virtual Bracket Bracket { get; set; }
}
}
然后我创建了 DTO 对象来表示它们。
namespace MyApp.Models
{
public class BracketDto
{
public int BracketId { get; set; }
public string BracketName { get; set; }
public List<TeamDto> Teams { get; set; }
}
public class TeamDto
{
public int TeamId { get; set; }
public string TeamName { get; set; }
public int BracketId { get; set; }
}
}
在我的 class 之一中,我用来查询数据库并投影结果数据,这就是我正在做的事情:
public List<BracketDto> GetBrackets()
{
var brackets = (from a in this.Context.Brackets
join b in this.Context.Teams on a.BracketId equals b.BracketId
select new BracketDto()
{
BracketId = a.BracketId,
BracketName = a.BracketName
}).ToList<BracketDto>();
var teams = (from a in this.Context.Teams
select new TeamDto()
{
TeamId = a.TeamId,
TeamName = a.TeamName,
BracketId = a.BracketId
}).ToList<TeamDto>();
foreach(BracketDto bracket in brackets)
{
var tl = teams.Where(a => a.BracketId == bracket.BracketId).ToList();
bracket.Teams = tl;
}
var xtra = (from a in this.Context.Brackets
join b in this.Context.Teams on a.BracketId equals b.BracketId
select new BracketDto()
{
BracketId = a.BracketId,
BracketName = a.BracketName
}).ToList<BracketDto>();
return brackets;
}
我在哪里获得结果:
[
{
"BracketId": 1,
"BracketName": "Eastern Conference",
"Teams": [
{
"TeamId": 1,
"TeamName": "Indiana Pacers",
"BracketId": 1
},
{
"TeamId": 3,
"TeamName": "Cleveland Cavaliers",
"BracketId": 1
}
]
},
{
"BracketId": 2,
"BracketName": "Western Conference",
"Teams": [
{
"TeamId": 2,
"TeamName": "Los Angeles Lakers",
"BracketId": 2
},
{
"TeamId": 4,
"TeamName": "Los Angeles Clippers",
"BracketId": 2
}
]
}
]
我知道可以有更有效的方法来做到这一点。但我不太确定如何在我的 LINQ 查询上执行此操作。
寻求意见和建议。谢谢!
所有的关系都准备好了,你只需要好好利用它们。
public List<BracketDto> GetBrackets()
{
var brackets = this.Context.Brackets.Select(a => {
return new BracketDto(){
BracketId = a.BracketId,
BracketName = a.BracketName,
Teams = a.Teams.Select(b => {
return new TeamDto() {
TeamId = b.TeamId,
TeamName = b.TeamName,
BracketId = b.BracketId
};
}).ToList()
};
}).ToList();
return brackets;
}