我的CarFormat對象具有Name和Id屬性。我正在提取所有的ClientId與我在內部傳遞方法相同的格式。我這樣做SelectListItem和linq any並選擇語句
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");
,但我得到的數據綁定數據綁定例外:`
「System.Collections中。 ObjectModel.ReadOnlyCollection1 [[xxxxx,xxxxxx, Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]'不包含 包含名稱爲'Name'的屬性。
當我投射從列表中一切正常,但我需要這個對所有對象不只是第一
myViewModel.CarFormats = new SelectList(carFormats.First(), "Id", "Name");
您正在返回包含ReadOnlyCollections的枚舉,也許您打算在x.CarFormat中獲取實際的汽車?可能SelectMany需要 – Dbuggy
@stuartd,是:) – user1765862
在你的linq查詢中是什麼類型的'x.CarFormat'。你正在使用'Any'。然後你選擇它。我在想這是一個列表。所以你的結果是一個列表的集合。 – Dbuggy