错误存在显式转换 C#,MVVM Light - LINQ
Error An explicit conversion exists C#, MVVM Light - LINQ
我正在学习 MVVM Light
,我正在开发的应用程序具有搜索事件名称的功能。这是我在用户输入 TextBox
时过滤 ListBox
的代码。
错误是:Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<NGO_Volunteer_Comm_Platform_v1._0.DataModel.Event>' to 'System.Collections.Generic.List<NGO_Volunteer_Comm_Platform_v1._0.DataModel.Event>'. An explicit conversion exists (are you missing a cast?)
ViewModel
代码:
private static ObservableCollection<Event> _searchEventCollection = new ObservableCollection<Event>();
public static ObservableCollection<Event> SearchEventCollection
{
get { return _searchEventCollection; }
set { _searchEventCollection = value; }
}
//search from homepage event section
private RelayCommand _eventSearch;
/// <summary>
/// Gets the EventSearch.
/// </summary>
public RelayCommand EventSearch
{
get
{
return _eventSearch
?? (_eventSearch = new RelayCommand(
async () =>
{
SearchEventCollection.Clear();
var eventList = await App.MobileService.GetTable<Event>().ToListAsync();
foreach (Event ename in eventList)
{
SearchEventCollection.Add(new Event
{
Id = ename.Id,
EventName = ename.EventName,
Date = ename.Date,
Location = ename.Location,
Desc = ename.Desc
});
}
}));
}
}
private string filter;
public String Filter
{
get
{
return this.filter;
}
set
{
this.filter = value;
RaisePropertyChanged("SearchEventCollection");
}
}
public List<Event> FilteredNames
{
get
{
return (from name in SearchEventCollection where name.EventName.StartsWith(filter) select name);
}
}
public searchfromhomepageViewModel()
{
filter = "";
}
如何解决此错误?
使用 ToList
扩展方法从 IEnumerable<T>
创建一个 List<T>
:
public List<Event> FilteredNames
{
get
{
return (from name in SearchEventCollection where name.EventName.StartsWith(filter) select name).ToList();
}
}
或将 FilteredNames
属性 的类型更改为 IEnumerable<Event>
。实际上,这可能是一个更好的主意。一般来说,您不应该公开具体的集合类型,因为如果您以后需要更改它,它不会让您 return 一个不同类型的实例。此外,returning a List<T>
似乎暗示您可以修改列表。但是由于每次调用 属性 时都会 returned 一个新列表,修改它不会对 属性 产生任何影响,所以它具有误导性。
我正在学习 MVVM Light
,我正在开发的应用程序具有搜索事件名称的功能。这是我在用户输入 TextBox
时过滤 ListBox
的代码。
错误是:Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<NGO_Volunteer_Comm_Platform_v1._0.DataModel.Event>' to 'System.Collections.Generic.List<NGO_Volunteer_Comm_Platform_v1._0.DataModel.Event>'. An explicit conversion exists (are you missing a cast?)
ViewModel
代码:
private static ObservableCollection<Event> _searchEventCollection = new ObservableCollection<Event>();
public static ObservableCollection<Event> SearchEventCollection
{
get { return _searchEventCollection; }
set { _searchEventCollection = value; }
}
//search from homepage event section
private RelayCommand _eventSearch;
/// <summary>
/// Gets the EventSearch.
/// </summary>
public RelayCommand EventSearch
{
get
{
return _eventSearch
?? (_eventSearch = new RelayCommand(
async () =>
{
SearchEventCollection.Clear();
var eventList = await App.MobileService.GetTable<Event>().ToListAsync();
foreach (Event ename in eventList)
{
SearchEventCollection.Add(new Event
{
Id = ename.Id,
EventName = ename.EventName,
Date = ename.Date,
Location = ename.Location,
Desc = ename.Desc
});
}
}));
}
}
private string filter;
public String Filter
{
get
{
return this.filter;
}
set
{
this.filter = value;
RaisePropertyChanged("SearchEventCollection");
}
}
public List<Event> FilteredNames
{
get
{
return (from name in SearchEventCollection where name.EventName.StartsWith(filter) select name);
}
}
public searchfromhomepageViewModel()
{
filter = "";
}
如何解决此错误?
使用 ToList
扩展方法从 IEnumerable<T>
创建一个 List<T>
:
public List<Event> FilteredNames
{
get
{
return (from name in SearchEventCollection where name.EventName.StartsWith(filter) select name).ToList();
}
}
或将 FilteredNames
属性 的类型更改为 IEnumerable<Event>
。实际上,这可能是一个更好的主意。一般来说,您不应该公开具体的集合类型,因为如果您以后需要更改它,它不会让您 return 一个不同类型的实例。此外,returning a List<T>
似乎暗示您可以修改列表。但是由于每次调用 属性 时都会 returned 一个新列表,修改它不会对 属性 产生任何影响,所以它具有误导性。