Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(); 
like image 383
Justin Adkins Avatar asked Dec 04 '13 16:12

Justin Adkins


People also ask

How to convert List to IEnumerable List?

You can use the extension method AsEnumerable in Assembly System. Core and System. Linq namespace : List<Book> list = new List<Book>(); return list.

Can we convert List to IEnumerable in C#?

We can use the AsEnumerable() function of LINQ to convert a list to an IEnumerable in C#.

What is IEnumerable SelectListItem?

SelectList(IEnumerable) Initializes a new instance of the SelectList class by using the specified items for the list. SelectList(IEnumerable, Object) Initializes a new instance of the SelectList class by using the specified items for the list and a selected value. SelectList(IEnumerable, Object, IEnumerable)

How do I add to IEnumerable?

What you can do is use the Add extension method to create a new IEnumerable<T> with the added value. var items = new string[]{"foo"}; var temp = items; items = items. Add("bar");


2 Answers

Maybe try this? (untested)

ViewBag.AvaiableEnums = dynamicTextEnumsAvaiable.Select(x =>                                    new SelectListItem()                                    {                                       Text = x.ToString()                                   }); 
like image 74
Ric Avatar answered Sep 27 '22 20:09

Ric


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

like image 25
NinjaNye Avatar answered Sep 27 '22 22:09

NinjaNye