Search code examples
linqentity-framework-4checkedlistbox

Create a string collection from CheckedListBox.CheckedItems using LINQ


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.


Solution

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

    _eventTypesCheckedList.CheckedItems.Cast<tblEventType>()