C#LINQ从具有多个字段的列表中选择表 [英] C# LINQ Select table from list with multiple fields

查看:350
本文介绍了C#LINQ从具有多个字段的列表中选择表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从带有列表的表中选择许多值. 我有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 = 12,& 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆