gpt4 book ai didi

c# - 加入两个模型后如何返回匿名类型

转载 作者:太空宇宙 更新时间:2023-11-03 21:17:12 26 4
gpt4 key购买 nike

我正在开发一个 asp.net mvc-5 网络应用程序。我有以下操作方法,它在 Controller 类中返回一个 ActionResult :-

public ActionResult AutoComplete(string term, string SearchBy)
{
if (SearchBy == "Tag")
{
var tech = repository.AllFindTechnolog(term.Trim()).ToList();
var resources = repository.GetResources(tech.Select(a => a.IT360ID.Value).ToArray(), false).ToList();
var query = from techItems in tech
join resourcesItems in resources
on techItems.IT360ID.Value equals resourcesItems.RESOURCEID // join based on db2ID
orderby techItems.PartialTag
select new { extra = true, label = techItems.Tag.ToString(), techtype = techItems.TechnologyType.Name, status = resourcesItems.ResourceState.DISPLAYSTATE, customername = resourcesItems.ResourceLocation.SiteDefinition.AccountDefinition.ORG_NAME.ToString(), resourcename = resourcesItems.RESOURCENAME.ToString(), sitename = resourcesItems.ResourceLocation.SiteDefinition.SDOrganization.NAME };

return Json(query, JsonRequestBehavior.AllowGet);
}
else
{
var activeResources = repository.FindActiveResourceByName(term.Trim(), true).ToList();//.OrderBy(p => p.RESOURCENAME).Select(a => new { label = a.RESOURCENAME }).ToList();
var resources = repository.GetResources(activeResources.Select(a => a.RESOURCEID).ToArray(), false).ToList();
var tech = repository.getTechnologiesByIT360ids(activeResources.Select(a=>a.RESOURCEID).ToArray()).ToList();
var query = from techItems in tech
join resourcesItems in resources
on techItems.IT360ID.Value equals resourcesItems.RESOURCEID // join based on db2ID
orderby techItems.Tag
select new { extra = true, label = resourcesItems.RESOURCENAME.ToString(), techtype = techItems.TechnologyType.Name, status = resourcesItems.ResourceState.DISPLAYSTATE, customername = resourcesItems.ResourceLocation.SiteDefinition.AccountDefinition.ORG_NAME.ToString(), resourcename = techItems.Tag.ToString(), sitename = resourcesItems.ResourceLocation.SiteDefinition.SDOrganization.NAME };

return Json(query, JsonRequestBehavior.AllowGet);

}


}

现在我需要在另一个 Controller 类上使用此方法。所以我尝试将此方法移至共享模型类,并从两个操作方法中引用此共享模型类。但我不确定我需要如何定义模型类方法定义。因为我不能/不应该从模型类返回 ActionResult 所以任何人都可以就此提出建议吗?

最佳答案

您可能可以清理一些,但让您了解您可以做什么。创建一个新类并将其命名为有意义的名称。我刚刚使用了 MyModel

public class MyModel
{
public bool extra { get; set; }
public string label { get; set; }
public string techtype { get; set; }
public string status { get; set; }
public string customername { get; set; }
public string resourcename { get; set; }
public string sitename { get; set; }

public static List<MyModel> AutoComplete(string term, string SearchBy)
{
using (var repository = new MyDataContext())
{
if (SearchBy == "Tag")
{
var tech = repository.AllFindTechnolog(term.Trim()).ToList();
var resources = repository.GetResources(tech.Select(a => a.IT360ID.Value).ToArray(), false).ToList();
var query = from techItems in tech
join resourcesItems in resources
on techItems.IT360ID.Value equals resourcesItems.RESOURCEID // join based on db2ID
orderby techItems.PartialTag
select new MyModel { extra = true, label = techItems.Tag.ToString(), techtype = techItems.TechnologyType.Name, status = resourcesItems.ResourceState.DISPLAYSTATE, customername = resourcesItems.ResourceLocation.SiteDefinition.AccountDefinition.ORG_NAME.ToString(), resourcename = resourcesItems.RESOURCENAME.ToString(), sitename = resourcesItems.ResourceLocation.SiteDefinition.SDOrganization.NAME };

return query.ToList();
}
else
{
var activeResources = repository.FindActiveResourceByName(term.Trim(), true).ToList();//.OrderBy(p => p.RESOURCENAME).Select(a => new { label = a.RESOURCENAME }).ToList();
var resources = repository.GetResources(activeResources.Select(a => a.RESOURCEID).ToArray(), false).ToList();
var tech = repository.getTechnologiesByIT360ids(activeResources.Select(a => a.RESOURCEID).ToArray()).ToList();
var query = from techItems in tech
join resourcesItems in resources
on techItems.IT360ID.Value equals resourcesItems.RESOURCEID // join based on db2ID
orderby techItems.Tag
select new MyModel { extra = true, label = resourcesItems.RESOURCENAME.ToString(), techtype = techItems.TechnologyType.Name, status = resourcesItems.ResourceState.DISPLAYSTATE, customername = resourcesItems.ResourceLocation.SiteDefinition.AccountDefinition.ORG_NAME.ToString(), resourcename = techItems.Tag.ToString(), sitename = resourcesItems.ResourceLocation.SiteDefinition.SDOrganization.NAME };

return query.ToList();
}
}
}
}

现在当你想在你的操作结果中使用它时,你可以调用

public ActionResult AutoComplete(string term, string SearchBy)
{
return Json(MyModel.AutoComplete(term, SearchBy),JsonRequestBehavior.AllowGet);
}

关于c# - 加入两个模型后如何返回匿名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262015/

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