.net MVC, SelectLists, and LINQ

▼魔方 西西 提交于 2019-12-05 03:33:06

The SelectList constructor takes an IEnumerable so all you need to do is pass the LINQ query to the constructor like so

 var query = from c in customers
                        select c;

 var customerList = new SelectList(query, "CustomerId", "CustomerName"); 

You should do this in the Controller and have the SelectList in your ViewModel.

You want to use the select keyword in the LINQ query:

var foo = new SelectList(from x in FooRepository.Items
                         select new SelectListItem { Text = x.Name, Value = x.Id });
    var foo = FoorePository.Items.Select(s = > new SelectListItem 
                                        {
                                          Text = s.Name, Value = s.Id.ToString()
                                        }
);

Sorry about formatting.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!