【发布时间】:2019-07-19 16:03:04
【问题描述】:
我想从带有列表的表中选择许多值。
我有FabricTable(年份是int):
+----+-------+---------+------+
| Id | Color | Texture | Year |
+----+-------+---------+------+
| 1 | Red | Rough | 2019 |
+----+-------+---------+------+
| 2 | Green | Soft | 2019 |
+----+-------+---------+------+
| 3 | Blue | Rough | 2019 |
+----+-------+---------+------+
| 4 | Red | Med | 2019 |
+----+-------+---------+------+
| 5 | Blue | Soft | 2018 |
+----+-------+---------+------+
我有selectedItems 列表(年份是int):
+---------+------+
| Texture | Year |
+---------+------+
| Rough | 2019 |
+---------+------+
| Soft | 2019 |
+---------+------+
我想从表中获取Id,结果应该是Id = 1, 2, & 3。
如何在 C# 中使用 Linq 实现这一点?我只需要选择Texture & Year
这是我尝试过的,但我不确定如何从具有多个值的列表中进行选择(selectedItems 是一个列表,但我不知道如何查询多个列):
db.FabricTable.Select(o => o.Texture == selectedItems.Texture && o.Year == selectItems.Year)
【问题讨论】:
-
你尝试了什么?什么没有奏效?在提出问题之前,您需要展示一些问题所在。
-
@BrunoBelmondo 我已经添加了我尝试过的内容,但在
selectedItems.Texture和selectedItems.Year上出现红色波浪线 -
Where子句将从表中选择项目,每个项目将包含所有属性,包括Id。要根据列表过滤项目,请参阅下面安迪的回答。
标签: c# linq asp.net-core entity-framework-core