【问题标题】:C# LINQ Select table from list with multiple fieldsC# LINQ 从具有多个字段的列表中选择表
【发布时间】: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.TextureselectedItems.Year 上出现红色波浪线
  • Where 子句将从表中选择项目,每个项目将包含所有属性,包括 Id。要根据列表过滤项目,请参阅下面安迪的回答。

标签: c# linq asp.net-core entity-framework-core


【解决方案1】:

使用selectedItems.Texture 时会出现编译器错误,因为selectedItem 是一个包含具有Texture 属性的对象的列表。在FabricTable中搜索所需项目时,您需要检查列表中的所有项目:

var items = db.FabricTable.Where(o => selectedItems.Any(selectedItem => o.Texture == selectedItem.Texture && o.Year == selectedItem.Year));

【讨论】:

  • 这个查询对我来说结果为 1 items,而它应该有很多结果。我想我应该把这个查询封装在foreach
  • 不,您不需要foreach。这将选择前三行。
  • @RufusL 在这里您可以看到selectedItems 有4 个计数,但items 只有1 个,我已经在数据库中验证了要删除的4 个项目。 i.imgur.com/XxJ1V6P.gif
  • 该数据与您在上面显示的数据不同。您只从第一个项目中选择值,而其他项目都没有与所有这三个属性完全匹配。
  • 这是我的错误,我有一个变量错误。谢谢!
【解决方案2】:

尝试以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication120
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Item> items = new List<Item>() { 
                new Item() { Id = 1, Color = "Red", Texture = "Rough",  Year = 2019},
                new Item() { Id = 2, Color = "Green", Texture = "Soft",  Year = 2019},
                new Item() { Id = 3, Color = "Blue", Texture = "Rough",  Year = 2019},
                new Item() { Id = 4, Color = "Red", Texture = "Soft",  Year = 2018}
            };
            DataTable dt = new DataTable();
            dt.Columns.Add("Color", typeof(string));
            dt.Columns.Add("Texture", typeof(string));
            dt.Columns.Add("Year", typeof(int));

            foreach (Item item in items.Where(x => (x.Texture == "Rough") && (x.Year == 2019)))
            {
                dt.Rows.Add(new object[] { item.Color, item.Texture, item.Year });
            }

        }

    }
    public class Item
    {
        public int Id { get; set; }
        public string Color { get; set; }
        public string Texture { get; set; }
        public int Year { get; set; }
    }

}

【讨论】:

  • 我不想通过硬编码值但通过列表中的字段进行搜索,我已经编辑了 OP 以明确我的意图
  • 所以只需更改为一个变量。
  • 该表有 Id 而不是列表,我正在尝试从带有列表的表中获取 Id
  • 列表有Id,而不是表格。如果你不把id放到表中你就不能得到它。
  • 我将问题读作“我有一个数据库表,我想将其用作列表的数据源”,尽管 OP 相当不清楚,因为标题相反。
猜你喜欢
  • 2010-11-15
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多