Create a string collection from CheckedListBox.CheckedItems using LINQ

Deadly 提交于 2019-12-13 04:39:28

问题


I have used the Entity framework to populate a checked list box. I want to get the names of the checked items as a string collection so that they can then be used as a filter for another LINQ query.

I populate the list box like this...

_eventTypesCheckedList.DataSource = this._dataContext.tblEventTypes.OrderBy(ev => ev.EventTypeName);
_eventTypesCheckedList.DisplayMember = "EventTypeName";

This is how I'm failing to get the string collection...

var types = from eType in ((_eventTypesCheckedList.CheckedItems) as IEnumerable< tblEventType > )
            select new string( eType.EventTypeName.ToCharArray() );

Any help would be great.


回答1:


Cast your CheckItems collection using .Cast<Type>() extension method:

_eventTypesCheckedList.CheckedItems.Cast<tblEventType>()


来源:https://stackoverflow.com/questions/10626107/create-a-string-collection-from-checkedlistbox-checkeditems-using-linq

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