PoliDev
PoliDev

Reputation: 1468

Using foreach with two types of conditions in c#

I used dropdown for search. The text value should different from the value. So, I created 2 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;

lstFunctions should come in the value field. How will I add this?

Upvotes: 5

Views: 1097

Answers (4)

Jodrell
Jodrell

Reputation: 35726

how 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 didn't want to instantiate 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;

Upvotes: 2

Wasif Hossain
Wasif Hossain

Reputation: 3950

You can first combine these 2 lists into one and then loop over that 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++;
}

Upvotes: 2

Enigmativity
Enigmativity

Reputation: 117154

You could use linq and get it done 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;

Upvotes: 4

Jaffa
Jaffa

Reputation: 12719

Instead of foreach you can use enumerators to iterate over the two list in 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++;
}

Upvotes: 3

Related Questions