如何在连接两个模型后返回匿名类型
本文关键字:模型 返回 类型 两个 连接 | 更新日期: 2023-09-27 18:18:37
我正在做一个asp.net mvc-5 web应用程序。我有以下的动作方法,返回一个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);
}
}
现在我需要在另一个控制器类上使用此方法。因此,我尝试将此方法移动到共享模型类,并从两个操作方法中引用此共享模型类。但是我不确定我需要如何定义模型类方法定义。因为我不能/不应该从模型类返回一个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);
}
构建一个类(视图模型)供任何对构建JSON结果感兴趣的代码使用,如下所示:
public class AutoCompleteViewModel
{
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; }
}
现在你可以使用AutoCompleteViewModel作为一个单独的方法的返回类型,这个方法构建/填充这个对象,或者在你的动作方法的LINQ查询中,像这样:
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
orderby techItems.PartialTag
select new AutoCompleteViewModel { 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 AutoCompleteViewModel { 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);
}
}
您可以使用dynamic作为共享模型类的返回类型。
样品(未测试):
public dynamic AutoCompleteService(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 query;
}
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 query;
}
}
并更改控制器的方法
public ActionResult AutoComplete(string term, string SearchBy)
{
return Json(AutoCompleteService(term, SearchBy), JsonRequestBehavior.AllowGet);
}