我需要清空IEnumerable列表,我尝试了很多方法,如null等,但它们都没有起作用。
这是我的模型的外观。
这是我的模型的外观。
public class NewsViewModel
{
public NewsViewModel()
{
this.Categories = new List<Category>();
}
public int NewsId { get; set; }
public string NewsTitle { get; set; }
public string NewsBody { get; set; }
public IEnumerable<Category> Categories { get; set; }
}
if (!string.IsNullOrEmpty(SelectedCategoriesIds))
{
List<Category> stringList = new List<Category>();
stringList.AddRange(SelectedCategoriesIds.Split(',').Select(i => new Category() { CategoryId = int.Parse(i) }));
model.Categories = stringList.AsEnumerable();
}
else
{
model.Categories = null;
}
如何使model.Categories
为空?
IEnumerable<T>
接口时,你的意图是下游使用这个接口来分发应该被枚举的东西。IEnumerable<T>
合同不包括任何与修改基础集合相关的成员。也许你应该暴露ICollection<T>
接口? - spenderList
转换为IEnumerable
?你不能在类中声明为List<T>
吗?有特定的原因吗? - techspider