简体   繁体   中英

Using foreach to iterate through Generic List and add Values into List<DTO>

I am getting some Values into one of my Generic List like this

List<IGroup> Details = Operations.GetAllGroups();

Now I have a DTO which I need to use as List<DTO> . Now I have to iterate through Details and populate values into List using foreach loop.

How to do it? I tried but i am not able to get fields of the Details List.

Update.. Tried like this..

 foreach (var v in Details) {

 group.Add(request.ActiveDirectoryGroupName = v.DisplayName, request.GroupDescription = v.Description, request.GroupTypeName = "", request.ApplicationIdentifier = "", request.ApplicationRightsDescription = "");

}

Here request is Calling the fields of the DTO.

I think this is want you want:

List<DTO> DTO = new List<DTO>;
foreach (IGroup ele in Details)
{
  DTO request = new DTO();
  // here you can extract details properties.
  request.ActiveDirectoryGroupName  = ele.DisplayName;
  ...
  ...
  DTO.Add(request);
}

Ofcourse you need to Edit as per your requirements.

This should work for you:

List<DTO> dtos = Details.Select(v => new DTO()
{
    ActiveDirectoryGroupName = v.DisplayName,
    GroupDescription = v.Description,
    GroupTypeName = "",
    ApplicationIdentifier = "",
    ApplicationRightsDescription = "",
}).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM