虽然迭代的IEnumerable&LT;串GT;填充列表&LT;&DTO GT; MVC C# [英] Iterate Though IEnumerable<String> to populate List<DTO> MVC C#
问题描述
任何人都可以有一个问题,我有帮助,我尝试写,将返回我的DTO对象的列表的方法
我的USR code和获取用户和得到他们的工作codeS在DB的列表。然后我得到所有的WCF作业的列表。然后,我想打一个新的列表与jobDescription连接的DTO为DB只有用户code和工作code。我要的是从我的WCF,但乔布斯的列表中的用户不必须JobDescription的列表。通过这个....
列表&LT; UserJobsDTO&GT;清单= listBuilder(工种,yetToHave);
目前我只有这一点,我设法让工作codeS名单可是没有一个用户,但我需要jobDescriptions ......谁能帮我完成我的方法?
公众的ActionResult addJob(字符串USR code)
{
VAR工作= jobsClient.GetAlljobs();
VAR alljobs code =(从s在就业选择s.jobs code).ToList();
VAR thisUserJob code =(从s在db.UserJobs
其中,s.usr code == USR code
选择s.jobs code).ToList();
VAR yetToHave = alljobs code.Except(thisUserJob code);
清单&LT; UserJobsDTO&GT;清单= listBuilder(工种,yetToHave);
ViewBag.jobs code =新的SelectList(列表中,工作code,jobDescription);
VAR模型=新UserJob {USR code = USR code};
返回视图(addJob模型);
} 私人列表&LT; UserJobsDTO&GT; listBuilder(jobsService.jobsDTO []的工作,IEnumerable的&LT;串GT; yetToHave)
{
的foreach(在yetToHave字符串s)
{
每yetToHave工作
附加描述UserJobsDTO.Description
}
}
DTO:
公共类UserJobsDTO
{
公共字符串用户code {搞定;组; }
公共字符串工作code {搞定;组; }
公共字符串jobDescription {搞定;组; }
}
您可以使用LINQ 包含
法!
(,表示不包含的)
假设 jobsClient.GetAlljobs()
返回 UserJobsDTO
DTO的集合。
//获取所有作业
VAR allJobs = jobsClient.GetAlljobs()了ToList()。//获取工作$ C $列表C此用户
VAR thisUsersJob codeLIST =(从s在db.UserJobs那里s.usr code == USR code
选择s.jobs code).ToList();//获取从所有工作项目除了在thisUsersJob codeLIST项目
VAR yetToHave = allJobs.Where(S = GT;!thisUsersJob codeLIST。载有(s.jobs code))了ToList()。//转换到一个列表&LT; SelectListItem&GT;对于下拉
VAR dropDownData = yetToHave.Select(S = GT;新SelectListItem
{值= s.jobs code,文本= s.jobDescription})了ToList()。ViewBag.jobs code = dropDownData;
Can anyone help with a problem i have, im trying to write a method that will return a list of my DTO objects
I am getting the user by the usrCode and and the getting a list of their jobCodes in the DB. Then i am getting a list of All Jobs from WCF. I then want to make a new list with the jobDescription attached the DTO as the DB only has userCode and jobCode. What i want is a list of JobDescription from my WCF but from a list of Jobs a user DOESNT have. via this....
List<UserJobsDTO> list = listBuilder(jobs, yetToHave);
Currently i have only this, i have managed to get a list of jobCodes a user doesnt have but i need jobDescriptions... Can anyone help me complete my method?
public ActionResult addJob(string usrCode)
{
var jobs = jobsClient.GetAlljobs();
var alljobsCode = (from s in jobs select s.jobsCode).ToList();
var thisUserJobCode = (from s in db.UserJobs
where s.usrCode == usrCode
select s.jobsCode).ToList();
var yetToHave = alljobsCode.Except(thisUserJobCode);
List<UserJobsDTO> list = listBuilder(jobs, yetToHave);
ViewBag.jobsCode = new SelectList(list, "jobsCode", "jobDescription");
var model = new UserJob { usrCode = usrCode };
return View("addJob", model);
}
private List<UserJobsDTO> listBuilder(jobsService.jobsDTO[] jobs, IEnumerable<string> yetToHave)
{
foreach (string s in yetToHave)
{
for every yetToHave job
attach description to UserJobsDTO.Description
}
}
DTO:
public class UserJobsDTO
{
public String userCode { get; set; }
public String jobsCode { get; set; }
public String jobDescription { get; set; }
}
You can use the LINQ Contains
method with !
( That means not contains)
Assuming jobsClient.GetAlljobs()
returns a collection of UserJobsDTO
DTO.
//Get all Jobs
var allJobs = jobsClient.GetAlljobs().ToList();
//Get a list of Job code this user
var thisUsersJobCodeList = (from s in db.UserJobs where s.usrCode == usrCode
select s.jobsCode).ToList();
//Get items from all jobs except the items in thisUsersJobCodeList
var yetToHave = allJobs.Where(s => !thisUsersJobCodeList .Contains(s.jobsCode)).ToList();
//Convert to a List<SelectListItem> for dropdown
var dropDownData = yetToHave.Select(s => new SelectListItem
{ Value = s.jobsCode, Text = s.jobDescription}).ToList();
ViewBag.jobsCode = dropDownData;
这篇关于虽然迭代的IEnumerable&LT;串GT;填充列表&LT;&DTO GT; MVC C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!