【发布时间】:2015-12-08 13:47:46
【问题描述】:
我的 CarFormat 对象具有 Name 和 Id 属性。我正在获取 ClientId 等于我在方法内部传递的所有格式。我就是这样的
var carFormats = this.carRepository.All()
.Where(x => x.CarFormat.Any(c => c.Car.Id == someVar.CarId))
.Select(x=>x.CarFormat).AsEnumerable();
这会返回一个 CarFormat 对象列表。
此对象具有属性Name 和Id。在我的视图模型中添加 SelectListItem CarFormats 作为属性后,我尝试将这个可枚举的资源列表绑定到选择列表中
myViewModel.CarFormats = new SelectList(carFormats, "Id", "Name");
但我收到数据绑定异常 DataBinding: `
'System.Collections.ObjectModel.ReadOnlyCollection1[[xxxxx, xxxxxx, 版本=1.0.0.0,文化=中性,PublicKeyToken=null]]' 没有 包含名为“Name”的属性。
当我预测只使用列表中的第一项时,一切正常,除了我需要对所有对象使用它,而不仅仅是第一个
myViewModel.CarFormats = new SelectList(carFormats.First(), "Id", "Name");
【问题讨论】:
-
您正在返回一个包含 ReadOnlyCollections 的可枚举,也许您的意思是在 x.CarFormat 中获取实际的汽车?那么可能需要 SelectMany
-
在您的 linq 查询中
x.CarFormat是什么类型。您正在使用Any。然后你选择它。我认为这是某种列表。所以你的结果是一个列表的集合。