我列出了与各种类的多个连接的LINQ查询的结果输出。这发生在数据访问层我需要将其转换为业务对象列表
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;
}
}
既然您已经为您想要的结果类型生成了字段,那么就使用它作为您的投影类型:
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块,因为吞下异常很少是一个好主意。要么做点什么,要么让它冒泡,在不同的层处理。