我有两个名为Buttons和Tiles的表,它们具有如下的多对多关系:
Buttons *-----------
ButtonID |
Title |
|
Tiles *-----------
TileID
Title
现在我试着像这样查询它:
b = new ObservableCollection<Buttons>(from x in db.Buttons
where x.Tile == SelectedTileObj
select x);
但是我得到如下所述的错误:
Unable to create a constant value of type 'Data.Tiles'.
Only primitive types or enumeration types are supported in this context.
假设TileID
是主键,您可以采用另一种方法:
- 选择你感兴趣的贴图(有正确的id)
-
使用导航属性,获得与上述磁贴相关联的按钮
var tiles = db.Tiles.Where(t => t.TileId == SelectedTileObj.TileId);
// t.Buttons should be the navigation property for retrieving associated buttons
var buttons = tiles.SelectMany(t => t.Buttons);
var b = new ObservableCollection<Buttons>(b);