C#LINQ从具有多个字段的列表中选择表 [英] C# LINQ Select table from list with multiple fields
问题描述
我想从带有列表的表中选择许多值.
我有FabricTable
(年份是int
):
I'd like to select many values from a table with a list.
I have FabricTable
(Year is 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
.
I'd like to get the Id
from table, it should result with Id
= 1
, 2
, & 3
.
如何在C#中使用Linq实现此目的?我只需要按Texture
选择Year
How can I achieve this with Linq in C#? I just need to select by Texture
& Year
这是我尝试过的方法,但是我不确定如何从具有多个值的列表中进行选择(selectedItems
是一个列表,但我不知道如何查询多个列):
Here's what I've tried but I'm not sure how to select from list with multiple values(selectedItems
is a list but I don't know how to query multiple columns):
db.FabricTable.Select(o => o.Texture == selectedItems.Texture && o.Year == selectItems.Year)
推荐答案
使用selectedItems.Texture
时会出现编译器错误,因为selectedItem
是包含具有Texture
属性的对象的列表.在FabricTable
:
You get a compiler error when using selectedItems.Texture
because selectedItem
is a list that contains an object with the Texture
property. You need to check all of the items in the list when searching for the desired items in FabricTable
:
var items = db.FabricTable.Where(o => selectedItems.Any(selectedItem => o.Texture == selectedItem.Texture && o.Year == selectedItem.Year));
这篇关于C#LINQ从具有多个字段的列表中选择表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!