I am working on these lists to get an item that matches the selected item from the combobox.
private void InitializaMessageElement()
{
if (_selectedTransactionWsName != null)
{
get a transaction webservice name matching the selected item from the drop down here the output=TestWS which is correct
var getTranTypeWsName = TransactionTypeVModel .GetAllTransactionTypes() .FirstOrDefault(transTypes => transTypes.WsMethodName == _selectedTransactionWsName);
Loop the list of wsnames from the treenode list. Here it gives me all the node I have which is correct.
var wsNameList = MessageElementVModel .GetAllTreeNodes().Select(ame => ame.Children).ToList();//. == getTranTypeWsName.WsMethodName);
find the getTranTypeWsName.WsMethodName in the wsNameList. Here is where I have the problem:
var msgElementList = wsNameList.Select(x => x.Where(ame => getTranTypeWsName != null && ame.Name == getTranTypeWsName.WsMethodName)).ToList();
my MsgElement list:
MsgElementObsList = new ObservableCollection<MessageElementViewModel>(msgElementList);
this.messageElements = _msgElementList;
NotifyPropertyChanged("MessageElements");
}
Here it is throwing the cast error. why is it not working? I am new to LINQ. thanks
Modify
to
This is because although all lists are enumerable, all enumerables are not lists, and this one happens not to be one.
Also, your bool error has to do with returning true in the select. Here's the fixed code for that:
As the error is trying to tell you, LINQ methods return special iterator types the implement
IEnumerable<T>
; they do not returnList<T>
.This enables deferred execution.
Since the object isn't actually a
List<T>
, you can't cast it to a type that it isn't.If you need a
List<T>
, you can either callToList()
, or skip LINQ entirely and useList<T>.ConvertAll()
, which is likeSelect()
, but does returnList<T>
.