我想问一下如何将多个模型对象映射到单个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; }
}
}
在我的一个班级中,我用来查询数据库并投影结果数据,这就是我在做的事情:
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查询上做到这一点。
寻找建议和意见。谢谢!
答案 0 :(得分:1)
您拥有所有关系,您只需要利用它们。
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;
}