gpt4 book ai didi

c# - ASP MVC API Controller 组合类结果

转载 作者:行者123 更新时间:2023-11-30 22:12:18 26 4
gpt4 key购买 nike

在 ASP MVC Api 上,我有树形命名函数来检索数据,但我想将它们组合到一个请求 StylesAll:{ },这是代码。

[ActionName("Styles")]
public IEnumerable<StyleDTO> GetStyles()
{
return from s in db.styles
select new StyleDTO() { Name = s.Name, StyleId = s.StyleId };

}


[ActionName("Labels")]
public IEnumerable<LabelDTO> GetLabels()
{
return from l in db.Labels
select new LabelDTO() { Name = l.Name, LabelId = l.LabelId , image = l.image};
}

[ActionName("Commodity")]
public IEnumerable<CommodityDTO> GetCommodity()
{
return from c in db.Commodities
select new CommodityDTO() { CommodityId = c.CommodityId, CreateDate = c.CreateDate, Name = c.Name, Varieties = ( from v in c.Varieties select new VarietyDTO()
{
CommodityId = v.CommodityId, Name = v.Name, VarietyId = v.VarietyId
}) };
}

最佳答案

创建一个 ViewModel 以包含所有数据,然后创建函数来填充 ViewModel 并返回它。

查看模型

public class AllStyles
{
public IEnumerable<StyleDTO> Commodities {get;set;}
public IEnumerable<LabelDTO> Labels {get;set;}
public IEnumerable<CommodityDTO> Styles {get;set;}
}

API 操作方法

[ActionName("StylesAll")]
public AllStyles GetAllStyles()
{
return new AllStyles{
Styles = from s in db.styles
select new StyleDTO() {
Name = s.Name,
StyleId = s.StyleId },
Labels = from l in db.Labels
select new LabelDTO() {
Name = l.Name,
LabelId = l.LabelId ,
image = l.image},
Commodities = from c in db.Commodities
select new CommodityDTO() {
CommodityId = c.CommodityId,
CreateDate = c.CreateDate,
Name = c.Name,
Varieties = ( from v in c.Varieties
select new VarietyDTO() {
CommodityId = v.CommodityId,
Name = v.Name,
VarietyId = v.VarietyId})}
};
}

关于c# - ASP MVC API Controller 组合类结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19759039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com