5

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?

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
PoliDev
  • 1,408
  • 9
  • 24
  • 45
  • maybe you can use a list of KeyValuePair where key is role and value is function? – mcy Mar 10 '14 at 09:27
  • 3
    how do you know which "role" goes with which "function"? – Eren Ersönmez Mar 10 '14 at 09:28
  • that depends on how you use your RolesForJobSeekers view data, if it is required by some framework you are using, name it so others can help, or consult it documentation. This is more than how to use that framework, than a c# foreach – Russell Yan Mar 10 '14 at 09:32
  • if you're asking how to iterate two lists in parallel, see [this answer](http://stackoverflow.com/a/18396163/201088). – Eren Ersönmez Mar 10 '14 at 09:38

4 Answers4

4

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;
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
3

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++;
}
Jaffa
  • 12,442
  • 4
  • 49
  • 101
2

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;
Jodrell
  • 34,946
  • 5
  • 87
  • 124
2

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++;
}
Wasif Hossain
  • 3,900
  • 1
  • 18
  • 20