我创建了以下视图模型:
public class PropertyViewModel
{
public PropertyViewModel(Property property, IList<PropertyImage> images)
{
this.property = property;
this.images = images;
}
public Property property { get; private set; }
public IList<PropertyImage> images { get; private set; }
}
现在我需要创建一个函数来获取数据库中的所有属性及其相关图像。是否可以使用上面的视图模型来做到这一点?我尝试了以下方法。
public IList<PropertyViewModel> GetAllPropertyViews()
{
IList<PropertyViewModel> properties = null;
foreach (var property in GetAllProperties().ToList())
{
IList<PropertyImage> images = db.PropertyImages.Where(m => m.Property.PropertyID == property.PropertyID).ToList();
properties.Add(new PropertyViewModel(property, images));
}
return properties;
}
这不起作用,它给出“对象引用未设置为对象的实例”。在properties.Add(new PropertyViewModel(property, images));
对于我正在使用的分页方法,我需要返回一个 IQueryable 变量。任何建议将不胜感激。