我有来自多个连接的各种类的LINQ Query的列表结果输出。 这发生在数据访问层,我需要将其转换为业务对象列表
namespace App.Business.Entities
{
public class GetAllFunction_VM
{
public GetAllFunction_VM()
{ }
[Required]
public int Function_ID { get; set; }
[StringLength(250)]
[Required]
public string FunctionName { get; set; }
public int Hierarchy_Level { get; set; }
[Required]
public int ParentID { get; set; }
[StringLength(250)]
[Required]
public string ParentFunctionName { get; set; }
public int ControllerID { get; set; }
[StringLength(250)]
[Required]
public string ControllerName { get; set; }
public int FunctionInControllerID { get; set; }
public int ActionID { get; set; }
[StringLength(250)]
[Required]
public string ActionName { get; set; }
public int FunctionInActionID { get; set; }
}
}
public List<FunctionNavigation_VM> GetAllFunctions()
{
List<FunctionNavigation_VM> FunctionNavigationListObject = new List<FunctionNavigation_VM>();
try
{
using (var _uow = new FunctionsNavigation_UnitOfWork())
{
var entities = (from functionNavigation in _uow.Sys_Nav_Functions_Repository.GetAll()
join functionHierarchy in _uow.Sys_Nav_FunctionHierarchy_Repository.GetAll() on functionNavigation.Function_ID equals functionHierarchy.Function_ID
join functionNavigation_b in _uow.Sys_Nav_Functions_Repository.GetAll() on functionHierarchy.Parent_Function_ID equals functionNavigation_b.Function_ID
into tmp from functionNavigation_b in tmp.DefaultIfEmpty()
join functionInController in _uow.Sys_Nav_FunctionInController_Repository.GetAll() on functionNavigation.Function_ID equals functionInController.Function_ID
join functionController in _uow.Sys_Nav_FunctionController_Repository.GetAll() on functionInController.ControllerID equals functionController.ControllerID
join functionInAction in _uow.Sys_Nav_FunctionInAction_Repository.GetAll() on functionNavigation.Function_ID equals functionInAction.Function_ID
join functionAction in _uow.Sys_Nav_FunctionAction_Repository.GetAll() on functionInAction.ActionID equals functionAction.ActionID
select new
{
functionNavigation.Function_ID,
FunctionName = functionNavigation.Title,
functionNavigation.Hierarchy_Level,
ParentID = functionHierarchy.Parent_Function_ID,
ParentFunctionName = functionNavigation_b.Title,
functionController.ControllerID,
functionController.ControllerName,
functionInController.FunctionInControllerID,
functionAction.ActionID,
functionAction.ActionName,
functionInAction.FunctionInActionID
}
);
//Map Data Entity to Business Entity
//need help here... Auto Mapper
return FunctionNavigationListObject ;
}
}
catch(Exception e)
{
return null;
}
}
答案 0 :(得分:1)
你已经为你想要的结果类型生成了字段,只需将其用作预计类型:
public List<FunctionNavigation_VM> GetAllFunctions()
{
using (var _uow = new FunctionsNavigation_UnitOfWork())
{
var entities = (from functionNavigation in _uow.Sys_Nav_Functions_Repository.GetAll()
join functionHierarchy in _uow.Sys_Nav_FunctionHierarchy_Repository.GetAll() on functionNavigation.Function_ID equals functionHierarchy.Function_ID
join functionNavigation_b in _uow.Sys_Nav_Functions_Repository.GetAll() on functionHierarchy.Parent_Function_ID equals functionNavigation_b.Function_ID
into tmp from functionNavigation_b in tmp.DefaultIfEmpty()
join functionInController in _uow.Sys_Nav_FunctionInController_Repository.GetAll() on functionNavigation.Function_ID equals functionInController.Function_ID
join functionController in _uow.Sys_Nav_FunctionController_Repository.GetAll() on functionInController.ControllerID equals functionController.ControllerID
join functionInAction in _uow.Sys_Nav_FunctionInAction_Repository.GetAll() on functionNavigation.Function_ID equals functionInAction.Function_ID
join functionAction in _uow.Sys_Nav_FunctionAction_Repository.GetAll() on functionInAction.ActionID equals functionAction.ActionID
select new FunctionNavigation_VM
{
functionNavigation.Function_ID,
FunctionName = functionNavigation.Title,
functionNavigation.Hierarchy_Level,
ParentID = functionHierarchy.Parent_Function_ID,
ParentFunctionName = functionNavigation_b.Title,
functionController.ControllerID,
functionController.ControllerName,
functionInController.FunctionInControllerID,
functionAction.ActionID,
functionAction.ActionName,
functionInAction.FunctionInActionID
}
);
return entities.ToList(); ;
}
}
请注意,我更改了投影以创建FunctionNavigation_VM
个对象并删除了try / catch块,因为吞下异常很少是一个好主意。要么对它做些什么,要么只是让它冒泡并在不同层处理它。