Justin Adkins
Justin Adkins

Reputation: 1234

Convert List to IEnumerable<SelectListItem>

Tricky problem here. I'm trying to convert items for a list to IEnumerable<SelectListItem>.

Lists

dynamicTextInDatabase simply returns all the Enums that are used in my database. Currently only returns 1 and 2. The data class simple holds an int value.

dynamicTextEnumsInDatabase is the same list as above just in the class that actually stores my Enums.

dynamicTextEnumsAll is a list of all the Enums that are available in my application. Currently: 1, 2 and 3.

dynamicTextEnumsAvaiable is a list of all the unused Enums. It combines All the Enums and then removes the ones that are in the database list.

I need to pass the dynamicTextEnumsAvaiable to my View via the ViewBag to display in a drop-down list. However, It must be IEnumerable<SelectListItem> rather than a List.

Code

        var dynamicTextInDatabase = new List<DynamicTextEnumData>();
        var dynamicTextEnumsInDatabase = new List<DynamicTextEnum>();
        var dynamicTextEnumsAll = new List<DynamicTextEnum>();
        IEnumerable<SelectListItem> list;

        using (var proxy = new Proxy<IDynamicText>())
        {
            dynamicTextInDatabase = proxy.Channel.DynamicTextGetActiveEnums(Helper.GetCallingUser());
        }

        foreach (DynamicTextEnumData item in dynamicTextInDatabase)
        {
            var dynamicTextEnum = (DynamicTextEnum)item.DynamicTextEnum;
            dynamicTextEnumsInDatabase.Add(dynamicTextEnum);
        }

        foreach (DynamicTextEnum item in Enum.GetValues(typeof(DynamicTextEnum)))
        {
            dynamicTextEnumsAll.Add(item);
        }

        var dynamicTextEnumsAvaiable = dynamicTextEnumsAll.Except(dynamicTextEnumsInDatabase).ToList();

        if (dynamicTextEnumsAvaiable.Count == 0)
        {
            TempData["Error"] = "To update the text of a message or warning, please select it from the grid below.";
            return RedirectToAction("Index");
        }

        ViewBag.AvaiableEnums = dynamicTextEnumsAvaiable;
        return View();

Upvotes: 42

Views: 100347

Answers (6)

Juan Rojas
Juan Rojas

Reputation: 8861

If you need to convert a List of objects (without Enums) to IEnumerable, you can use a declarative style of LINQ:

var items = from TipoDoc t in tipoDocs
                select new SelectListItem {Value = t.Id, Text = t.Description};

Here, tipoDocs is a List of objects of type TipoDoc

public class TipoDoc
{
    public string Id { get; set; }
    public string Description { get; set; }
}

Upvotes: 1

Just use .AsEnumerable() method.

Example:

IEnumerable<SelectListItem> myCollection = dynamicTextEnumsAvaiable.AsEnumerable()

Check out here: https://msdn.microsoft.com/en-us/library/system.data.datatableextensions.asenumerable(v=vs.110).aspx

Upvotes: -1

Adam Garner
Adam Garner

Reputation: 1286

have you tried

IEnumerable<SelectListItem> myCollection = dynamicTextEnumsAvaiable.AsEnumerable()

Upvotes: -1

NinjaNye
NinjaNye

Reputation: 7126

You could do the following

ViewBag.AvaiableEnums = new SelectList(dynamicTextEnumsAvaiable)

See http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist(v=vs.118).aspx

Upvotes: 18

David Pilkington
David Pilkington

Reputation: 13618

You can maybe use a Linq statement to convert it

IEnumerable<SelectListItem> myCollection = dynamicTextEnumsAvaiable
                                           .Select(i => new SelectListItem()
                                                        {
                                                            Text = i.ToString(), 
                                                            Value = i
                                                        });

Upvotes: 15

Ric
Ric

Reputation: 13248

Maybe try this? (untested)

ViewBag.AvaiableEnums = dynamicTextEnumsAvaiable.Select(x => 
                                  new SelectListItem() 
                                  {
                                      Text = x.ToString()
                                  });

Upvotes: 88

Related Questions