如何将 C# 中两个 DTO 的结果组合成一种类型的列表
How to combine the results of two DTOs in C# into one list of one type
我有一个 C# 网络应用程序,目前正在从两个数据库中提取数据。其中一个数据库有一个名为 table 的资源,其中包含员工详细信息,另一个数据库包含对在公司网络应用程序中收集的详细信息的视图。
我需要为每个 'active' 员工创建一个列表,其中还列出了他们的网络应用程序历史记录。
我想 return 类型为 ResourceDTO 的列表,每个用户在另一个列表中包含 WebAppDTO 的结果。
理想情况下,我将能够在 Angular 中进行操作,创建一个 table 列出员工姓名,并在下一列 "tally" 中列出他们参与的次数。每个计数都将是一个 link,然后使用 webappID 显示该员工的详细信息。
C# Question for Satckoverflow
//Web Api
public List<ResourceDTO> GetWebAppLeaders()
{
List<ResourceDTO> webappLeaders = new List<ResourceDTO>();
//Linq to get a list of employees that take part of Web App
var people = from x in _staffCtx.Resources
where (x.Active.HasValue && x.Active.Value && x.ManagerID.HasValue && x.FirstName.Length > 0)
select new ResourceDTO()
{
FirstName = x.FirstName,
LastName = x.LastName,
ResourceID = x.ResourceID,
};
webappLeaders = people.ToList();
//currently loops through each member of staff in list above and grabs their web app history.
foreach (var person in people)
{
int personID = person.ResourceID;
var webappHistory = from u in _webappCtx.CompanyWebAppViews
where (u.PupilID == personID || u.TeacherID == personID)
select new WebAppDTO()
{
webappDate = u.webappDate.ToString(),
webappID = u.ID,
};
webappHistory.ToList();
}
//webappLeaders.ToList();
// successfully returns list of Employees as filtered out by where clause.
return webappLeaders;
}
'//ResourceDTO.cs
public class ResourceDTO
{
public int ResourceID { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string JobTitle { get; set; }
public string ExtensionNumber { get; set; }
public string MobileNumber { get; set; }
public string MobileSpeedDial { get; set; }
public string IMAddress { get; set; }
public int? ManagerID { get; set; }
public string ManagerName { get; set; }
public string DepartmentType { get; set; }
// Other field you may need
//web app specific
public List<WWebAppDTO> webappPupil { get; set; }
public List<WebAppsDTO> webappTeacher { get; set; }
}
//WebAppDTO.cs
public class WebAppDTO
{
public int webappID { get; set; }
public string pupilName { get; set; }
public string teacherName { get; set; }
public string webappDate { get; set; }
public string pupilLearnt { get; set; }
public string pupilComments { get; set; }
public string teacherComments { get; set; }
}
如果我未能理解一些重要概念,请提前致歉,C# MVC 对我来说是新手。
我将不胜感激一些示例代码。
谢谢。
也许我误解了这个问题,但据我所知,您只需要通过在 for 循环中查询来初始化这两个列表?
foreach (var person in people)
{
int personID = person.ResourceID;
person.webappPupil = from u in _webappCtx.CompanyWebAppViews
where (u.PupilID == personID)
select new WebAppDTO()
{
webappDate = u.webappDate.ToString(),
webappID = u.ID,
}.ToList();
person.webappTeacher = from u in _webappCtx.CompanyWebAppViews
where (u.TeacherID == personID)
select new WebAppDTO()
{
webappDate = u.webappDate.ToString(),
webappID = u.ID,
}.ToList();
}
对代码的以下更改解决了我的问题。
//WebApi
public List<ResourceDTO> GetWebAppLeaders()
{
List<ResourceDTO> webappLeaders = new List<ResourceDTO>();
//Linq to get a list of employees that take part of WebApp
var people = from x in _staffCtx.Resources
where (x.Active.HasValue && x.Active.Value && x.ManagerID.HasValue && x.FirstName.Length > 0)
select new ResourceDTO()
{
FirstName = x.FirstName,
LastName = x.LastName,
ResourceID = x.ResourceID,
};
webappLeaders = people.ToList();
foreach (var person in webappLeaders)
{
int personID = person.ResourceID;
person.webappPupil = from u in _webappCtx.CompanyWebAppViews
where (u.PupilID == personID)
select new WebappDTO()
{
webappID = u.ID,
};
person.webappPupil.ToList();
person.webappTeacher = from u in _webappCtx.CompanyWebAppViews
where (u.TeacherID == personID)
select new WebappDTO()
{
webappID = u.ID,
};
person.webappTeacher.ToList();
}
return webappLeaders.ToList();
}
//ResourceDTO
public class ResourceDTO
{
public int ResourceID { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string JobTitle { get; set; }
public string ExtensionNumber { get; set; }
public string MobileNumber { get; set; }
public string MobileSpeedDial { get; set; }
public string IMAddress { get; set; }
public int? ManagerID { get; set; }
public string ManagerName { get; set; }
public string DepartmentType { get; set; }
//WebApp specific
public IQueryable<WebAppDTO> webappPupil { get; set; }
public IQueryable<WebAppDTO> webappTeacher { get; set; }
}
//WebAppDTO
public class WebAppDTO
{
public int webappID { get; set; }
public string pupilName { get; set; }
public string teacherName { get; set; }
public string webappDate { get; set; }
public string pupilLearnt { get; set; }
public string pupilComments { get; set; }
public string teacherComments { get; set; }
public List<WebAppDTO> WebApp { get; set; }
}
扩展 Stephen Vakil 的回答,for each 循环抱怨 IQeuryable 类型转换为 List 类型。为了解决这两个在 ResourceDTO 中创建的 IQueryable 类型的方法,然后能够将结果转换为列表,在 WebAppDTO 中创建了一个 tpye List 方法,最后一步涉及执行分配 webappLeaders = people.ToList()并将其送入 foreach 循环。现在的结果是 return 一个员工列表,其中包含他们的 Web 应用程序历史记录的更多列表。
我有一个 C# 网络应用程序,目前正在从两个数据库中提取数据。其中一个数据库有一个名为 table 的资源,其中包含员工详细信息,另一个数据库包含对在公司网络应用程序中收集的详细信息的视图。
我需要为每个 'active' 员工创建一个列表,其中还列出了他们的网络应用程序历史记录。
我想 return 类型为 ResourceDTO 的列表,每个用户在另一个列表中包含 WebAppDTO 的结果。
理想情况下,我将能够在 Angular 中进行操作,创建一个 table 列出员工姓名,并在下一列 "tally" 中列出他们参与的次数。每个计数都将是一个 link,然后使用 webappID 显示该员工的详细信息。
C# Question for Satckoverflow
//Web Api
public List<ResourceDTO> GetWebAppLeaders()
{
List<ResourceDTO> webappLeaders = new List<ResourceDTO>();
//Linq to get a list of employees that take part of Web App
var people = from x in _staffCtx.Resources
where (x.Active.HasValue && x.Active.Value && x.ManagerID.HasValue && x.FirstName.Length > 0)
select new ResourceDTO()
{
FirstName = x.FirstName,
LastName = x.LastName,
ResourceID = x.ResourceID,
};
webappLeaders = people.ToList();
//currently loops through each member of staff in list above and grabs their web app history.
foreach (var person in people)
{
int personID = person.ResourceID;
var webappHistory = from u in _webappCtx.CompanyWebAppViews
where (u.PupilID == personID || u.TeacherID == personID)
select new WebAppDTO()
{
webappDate = u.webappDate.ToString(),
webappID = u.ID,
};
webappHistory.ToList();
}
//webappLeaders.ToList();
// successfully returns list of Employees as filtered out by where clause.
return webappLeaders;
}
'//ResourceDTO.cs
public class ResourceDTO
{
public int ResourceID { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string JobTitle { get; set; }
public string ExtensionNumber { get; set; }
public string MobileNumber { get; set; }
public string MobileSpeedDial { get; set; }
public string IMAddress { get; set; }
public int? ManagerID { get; set; }
public string ManagerName { get; set; }
public string DepartmentType { get; set; }
// Other field you may need
//web app specific
public List<WWebAppDTO> webappPupil { get; set; }
public List<WebAppsDTO> webappTeacher { get; set; }
}
//WebAppDTO.cs
public class WebAppDTO
{
public int webappID { get; set; }
public string pupilName { get; set; }
public string teacherName { get; set; }
public string webappDate { get; set; }
public string pupilLearnt { get; set; }
public string pupilComments { get; set; }
public string teacherComments { get; set; }
}
如果我未能理解一些重要概念,请提前致歉,C# MVC 对我来说是新手。
我将不胜感激一些示例代码。
谢谢。
也许我误解了这个问题,但据我所知,您只需要通过在 for 循环中查询来初始化这两个列表?
foreach (var person in people)
{
int personID = person.ResourceID;
person.webappPupil = from u in _webappCtx.CompanyWebAppViews
where (u.PupilID == personID)
select new WebAppDTO()
{
webappDate = u.webappDate.ToString(),
webappID = u.ID,
}.ToList();
person.webappTeacher = from u in _webappCtx.CompanyWebAppViews
where (u.TeacherID == personID)
select new WebAppDTO()
{
webappDate = u.webappDate.ToString(),
webappID = u.ID,
}.ToList();
}
对代码的以下更改解决了我的问题。
//WebApi
public List<ResourceDTO> GetWebAppLeaders()
{
List<ResourceDTO> webappLeaders = new List<ResourceDTO>();
//Linq to get a list of employees that take part of WebApp
var people = from x in _staffCtx.Resources
where (x.Active.HasValue && x.Active.Value && x.ManagerID.HasValue && x.FirstName.Length > 0)
select new ResourceDTO()
{
FirstName = x.FirstName,
LastName = x.LastName,
ResourceID = x.ResourceID,
};
webappLeaders = people.ToList();
foreach (var person in webappLeaders)
{
int personID = person.ResourceID;
person.webappPupil = from u in _webappCtx.CompanyWebAppViews
where (u.PupilID == personID)
select new WebappDTO()
{
webappID = u.ID,
};
person.webappPupil.ToList();
person.webappTeacher = from u in _webappCtx.CompanyWebAppViews
where (u.TeacherID == personID)
select new WebappDTO()
{
webappID = u.ID,
};
person.webappTeacher.ToList();
}
return webappLeaders.ToList();
}
//ResourceDTO
public class ResourceDTO
{
public int ResourceID { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string JobTitle { get; set; }
public string ExtensionNumber { get; set; }
public string MobileNumber { get; set; }
public string MobileSpeedDial { get; set; }
public string IMAddress { get; set; }
public int? ManagerID { get; set; }
public string ManagerName { get; set; }
public string DepartmentType { get; set; }
//WebApp specific
public IQueryable<WebAppDTO> webappPupil { get; set; }
public IQueryable<WebAppDTO> webappTeacher { get; set; }
}
//WebAppDTO
public class WebAppDTO
{
public int webappID { get; set; }
public string pupilName { get; set; }
public string teacherName { get; set; }
public string webappDate { get; set; }
public string pupilLearnt { get; set; }
public string pupilComments { get; set; }
public string teacherComments { get; set; }
public List<WebAppDTO> WebApp { get; set; }
}
扩展 Stephen Vakil 的回答,for each 循环抱怨 IQeuryable 类型转换为 List 类型。为了解决这两个在 ResourceDTO 中创建的 IQueryable 类型的方法,然后能够将结果转换为列表,在 WebAppDTO 中创建了一个 tpye List 方法,最后一步涉及执行分配 webappLeaders = people.ToList()并将其送入 foreach 循环。现在的结果是 return 一个员工列表,其中包含他们的 Web 应用程序历史记录的更多列表。