Using foreach with two types of conditions in C #

I used the drop-down list to search. The value of the text must be different from the value. So, I created two types of methods:

    List<string> lstRoles = new List<string>();
    lstRoles = _repository.GetRolesForFindJobseekers();
    List<string> lstFunctions = new List<string>();
    lstFunctions = _repository.GetFunctionsForRolesFindJobSeekers();


    List<SelectListItem> selectListRoles = new List<SelectListItem>();

    int i = 1;
    foreach (string role in lstRoles)
    {

            selectListRoles.Add(new SelectListItem
            {
                Text = role,
                Value = role,
                Selected = (i == 0)
            });

            i++;

    }
    ViewData["RolesForJobSeekers"] = selectListRoles;

lstFunctionsshould appear in the value field. How can i add this?

+4
source share
4 answers

First you can combine these 2 lists into one, and then iterate over this list.

var lstCombined =
    lstRoles
     .Zip(lstFunctions, (role, function) => new {Role = role, Function = function}).ToList();

int i = 1;

foreach (var item in lstCombined)
{
    selectListRoles.Add(new SelectListItem
                            {
                                Text = item.Role,
                                Value = item.Function,
                                Selected = (i == 0)
                            });

    i++;
}
+1
source

You can use linq and do it in one query:

var selectListRoles =
    lstRoles
        .Zip(lstFunctions, (role, function) => new { role, function })
        .Select((rf, i) => new SelectListItem()
        {
            Text = rf.role,
            Value = rf.function,
            Selected = (i + 1 == 0),
        })
        .ToList();

ViewData["RolesForJobSeekers"] = selectListRoles;
+4
source

Instead of foreach, you can use counters to iterate over two lists at the same time

IEnumerator enum1 = lstRoles.GetEnumerator();
IEnumerator enum2 = lstFunctions.GetEnumerator();

int i = 1;
while ((enum1.MoveNext()) && (enum2.MoveNext()))
{
        selectListRoles.Add(new SelectListItem
        {
            Text = enum1.Current,
            Value = enum2.Current,
            Selected = (i == 0)
        });

        i++;
}
+3
source

What about

var selectListRoles = _repository.GetRolesForFindJobseekers().Zip(
    _repository.GetFunctionsForRolesFindJobSeekers(),
    (role, function) => new SelectListItem
        {
            Text = role,
            Value = function,
            Selected = false
        }).ToList();
selectListRoles[0].Selected = true;
ViewData["RolesForJobSeekers"] = selectListRoles;

If you do not want to create an instance selectListRoles

// If you know selectListRoles starts empty, use 0 instead of baseIndex.
var baseIndex = selectListRoles.Count;
selectListRoles.AddRange(_repository.GetRolesForFindJobseekers().Zip(
    _repository.GetFunctionsForRolesFindJobSeekers(),
    (role, function) => new SelectListItem
        {
            Text = role,
            Value = function,
            Selected = false
        }));
selectListRoles[baseIndex].Selected = true;
+2
source

Source: https://habr.com/ru/post/1530939/


All Articles