私は観測可能なコレクションを持っています。要素を削除したいです。私はlinq。どこの構文を使用しようとしましたが、私はキャスティングに固執しています。linq式にコードを最適化
public class Atc
{
public string Code { get; set; }
public string Description { get; set; }
public string ParentDescriptions { get; set; }
}
私は
public ObservableCollection<Atc> MedicinesObservableCollection
、医薬品の監視可能なコレクションを定義していると私は作品のコレクションから項目をフィルタリングする方法を持っている:
private void RemoveMedicineListItem(string code)
{
var filteredCollection = new ObservableCollection<Atc>();
foreach (var item in MedicinesObservableCollection)
{
if (item.Code != code)
{
filteredCollection.Add(item);
}
}
MedicinesObservableCollection = filteredCollection;
}
私が試してみました:
(ObservableCollection<Atc>)MedicinesObservableCollection.Where(x => x.Code != code);
これは実行時例外を取得します System.InvalidCastException: '型' WhereEnumerableIterator1 [Atc] 'のオブジェクトをキャストして' System.Collections.ObjectModel.ObservableCollection1 [Atc] 'と入力できません。
これはlinqの列挙型と関係がありますが、私は自分の深みから外れています。
コンストラクタを使用します。['ObservableCollection(IEnumerable )'](https://msdn.microsoft.com/en-us/library/cc679169(v=vs.110).aspx) –
Sinatr