将 collectionviewsource 转换为 WPF 中的列表
Cast collectionviewsource to list in WPF
我有静态方法需要传递过滤后的 collectionviewsource 的值,但目前我无法这样做。我尝试了各种方法将其转换为列表,但我无法适应静态方法。
ViewModel - 静态方法
static public List<string> getallcrsp()
{
List<string> data = new List<string>();
data.Add("TEST1");
data.Add("TEST2");
return data;
}
ViewModel - Collectionviewsource 方法 - AllCRSP 是一个 collectionviewsource,我希望将其转换为列表并将其值传递给静态方法。
public void Updateswlist()
{
CRmappings2 = new ObservableCollection<SPFetchCREntity>(crentities.ToList());
AllCRSP = CollectionViewSource.GetDefaultView(CRmappings2);
SearchMU = SelectedSW.SW_Version;
AllCRSP.Filter = obj =>
{
SPFetchCREntity entity = obj as SPFetchCREntity;
return obj != null && entity.SW_Version == SearchMU.ToString();
};
AllCRSP.Refresh();
Isdrawopen = false;
SelectedTabIndex = 0;
Isselected = false;
Isselected = true;
}
如果您有 CollectionViewSource
个 SPFetchCREntity
个对象并且想要 List<string>
个,您应该这样做:
using System.Linq;
// 1. Get source collection of the CollectionViewSource
IEnumerable source = AllCRSP.SourceCollection;
// 2. Make it generic using Linq OfType<> method
IEnumerable<SPFetchCREntity> source_typed = source.OfType<SPFetchCREntity>();
// 3. You can filter your list using Linq Where method
IEnumerable<SPFetchCREntity> source_typed_filtered = source_typed.Where(obj => obj != null && entity.SW_Version == SearchMU.ToString());
// 4. Get string equivalent of your objects using Linq Select method
IEnumerable<string> source_string = source_typed_filtered.Select(obj => <...something like obj.ToString()...>);
// 5. Convert it ToList using Linq
List<string> list = source_string.ToList();
或者用一个简短的句子
var list = AllCRSP.SourceCollection.OfType<SPFetchCREntity>().Where(...).Select(...).ToList();
我有静态方法需要传递过滤后的 collectionviewsource 的值,但目前我无法这样做。我尝试了各种方法将其转换为列表,但我无法适应静态方法。
ViewModel - 静态方法
static public List<string> getallcrsp()
{
List<string> data = new List<string>();
data.Add("TEST1");
data.Add("TEST2");
return data;
}
ViewModel - Collectionviewsource 方法 - AllCRSP 是一个 collectionviewsource,我希望将其转换为列表并将其值传递给静态方法。
public void Updateswlist()
{
CRmappings2 = new ObservableCollection<SPFetchCREntity>(crentities.ToList());
AllCRSP = CollectionViewSource.GetDefaultView(CRmappings2);
SearchMU = SelectedSW.SW_Version;
AllCRSP.Filter = obj =>
{
SPFetchCREntity entity = obj as SPFetchCREntity;
return obj != null && entity.SW_Version == SearchMU.ToString();
};
AllCRSP.Refresh();
Isdrawopen = false;
SelectedTabIndex = 0;
Isselected = false;
Isselected = true;
}
如果您有 CollectionViewSource
个 SPFetchCREntity
个对象并且想要 List<string>
个,您应该这样做:
using System.Linq;
// 1. Get source collection of the CollectionViewSource
IEnumerable source = AllCRSP.SourceCollection;
// 2. Make it generic using Linq OfType<> method
IEnumerable<SPFetchCREntity> source_typed = source.OfType<SPFetchCREntity>();
// 3. You can filter your list using Linq Where method
IEnumerable<SPFetchCREntity> source_typed_filtered = source_typed.Where(obj => obj != null && entity.SW_Version == SearchMU.ToString());
// 4. Get string equivalent of your objects using Linq Select method
IEnumerable<string> source_string = source_typed_filtered.Select(obj => <...something like obj.ToString()...>);
// 5. Convert it ToList using Linq
List<string> list = source_string.ToList();
或者用一个简短的句子
var list = AllCRSP.SourceCollection.OfType<SPFetchCREntity>().Where(...).Select(...).ToList();