我试图用控制器列出一些食品项目。我对另一个程序集中的数据使用UnitOfWork的存储库模式,并在BaseApiController中引用它。Data属性是我的UnitOfWork实例。
var result = Data.Food
.FindAll()
.Select(FoodItemViewModel.Create);
return result;
这里是我的ViewModel:
public static Expression<Func<FoodItem, FoodItemViewModel>> Create
{
get
{
return fi => new FoodItemViewModel
{
Id = fi.Id,
Description = fi.Description,
DiaryEntries = fi.DiaryEntries
.Select(s => new DiaryEntityViewModel()
{
Id = s.Id,
Quantity = s.Quantity
}
};
}
}
但我得到的是:
"在LINQ to中不支持指定的类型成员'DiaryEntries'实体。只有初始化式、实体成员和实体导航支持属性。"
My DiaryEntries成员在ViewModel是
IEnumerable<DiaryEntityViewModel>
,并且我的Data实例中的DiaryEntries成员是
IRepository<DiaryEntry>
和DiaryEntry是我的模型类
下面是我的FoodItem模型类:
public class FoodItem
{
private IEnumerable<Measure> measures;
private IEnumerable<DiaryEntry> diaryEntries;
public FoodItem()
{
this.measures = new HashSet<Measure>();
this.diaryEntries = new HashSet<DiaryEntry>();
}
public int Id { get; set; }
public string Description { get; set; }
public virtual IEnumerable<DiaryEntry> DiaryEntries
{
get
{
return this.diaryEntries;
}
set
{
this.diaryEntries = value;
}
}
public virtual IEnumerable<Measure> Measures
{
get
{
return this.measures;
}
set
{
this.measures = value;
}
}
}
将您的FoodItem
类更改为下面的类型, IEnumerable<T>
不支持作为导航集合的类型:
public class FoodItem
{
public FoodItem()
{
this.Measures = new HashSet<Measure>();
this.DiaryEntries = new HashSet<DiaryEntry>();
}
public int Id { get; set; }
public string Description { get; set; }
public virtual ICollection<DiaryEntry> DiaryEntries
{
get;
set;
}
public virtual ICollection<Measure> Measures
{
get;
set;
}
}