我有3个模型,它们分别是:
Pencil,具有 Pencil.Id(int) 和 Pencil.Colors(IEnumerable) 属性。
Pen,具有 Pen.Id(int) 和 Pen.Colors(IEnumerable) 属性。
Colors,具有 Id 和 name 属性。
Pencil 与颜色相关(多对多关系), Pen 与颜色相关(多对多关系)。
我想构建一个查询,它将显示与我手中的笔具有相同颜色的铅笔。
我正在使用以下 LINQ-to-Entities 查询:
int id = id_of_the_pen_that_i_am_holding;
Pen p = db.Pens.Find(id);
var list = from m in db.Pencils where m.Colors.Intersect(p.Colors) != null select m;
颜色模型是IEnumerable类型,因此它有多种颜色。例如,钢笔有15种不同的颜色,而铅笔则有25种不同的颜色。如果我手中的钢笔中有一种颜色也在铅笔的颜色调色板中存在,我想得到对应的铅笔。
但是我却遇到了一个异常,提示我使用普通变量,如int或string,而不是对象。
我该怎么办?感谢您的帮助。
编辑过:我已经为可能出现的下一个问题创建了一个新的问题:C# LINQ to Entities- Properties on the intersection of an object and a collection of objects