Convert List to IEnumerable

前端 未结 6 1391
囚心锁ツ
囚心锁ツ 2020-12-29 01:59

Tricky problem here. I\'m trying to convert items for a list to IEnumerable.

Lists

dynamicTextInDatabase si

相关标签:
6条回答
  • 2020-12-29 02:17

    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; }
    }
    
    0 讨论(0)
  • 2020-12-29 02:21

    have you tried

    IEnumerable<SelectListItem> myCollection = dynamicTextEnumsAvaiable.AsEnumerable()
    
    0 讨论(0)
  • 2020-12-29 02:25

    Maybe try this? (untested)

    ViewBag.AvaiableEnums = dynamicTextEnumsAvaiable.Select(x => 
                                      new SelectListItem() 
                                      {
                                          Text = x.ToString()
                                      });
    
    0 讨论(0)
  • 2020-12-29 02:26

    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

    0 讨论(0)
  • 2020-12-29 02:26

    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

    0 讨论(0)
  • 2020-12-29 02:29

    You can maybe use a Linq statement to convert it

    IEnumerable<SelectListItem> myCollection = dynamicTextEnumsAvaiable
                                               .Select(i => new SelectListItem()
                                                            {
                                                                Text = i.ToString(), 
                                                                Value = i
                                                            });
    
    0 讨论(0)
提交回复
热议问题