【问题标题】:C# Searching List of custom objects for specific string within object [duplicate]C#在对象中搜索特定字符串的自定义对象列表[重复]
【发布时间】:2020-11-19 15:30:24
【问题描述】:

我正在尝试在对象列表中搜索所述对象的特定“特征”

以下是为列表创建对象的类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dice_Roller_v3._14
{
    public class ImageBase
    {
        public Bitmap Image { get; set; }
        public string Name { get; set; }
    }
    public class ColorBase
    {
        public Color Color { get; set; }
        public string Name { get; set; }
    }

这是从构造函数创建对象列表的代码。

    public partial class frmImage_Layer : Form
    {
        //Create lists for storing colors and images
        public List<ColorBase> ImgColor = new List<ColorBase>();
        //public List<String> ImgPath = new List<string>();
        public List<ImageBase> ImgBase = new List<ImageBase>();
        public frmImage_Layer()
        {
            InitializeComponent();
        }

此代码包含如何分配列表的示例

        private void btnImageControl_Control_Click(object sender, EventArgs e)
        {
            if (ErrorHandling("control") == true)
            {
                return;
            }
            if (radImageControl_Color_Add.Checked == true)
            {
                cpImageControl.ShowDialog();
                ImgColor.Add(new ColorBase(){Name = txtImageControl_Name.Text,Color = cpImageControl.Color});
                ArraysChanged();
            }
            else if (radImageControl_Color_Delete.Checked == true)
            {

            }
            else if (radImageControl_Part_Add.Checked == true)
            {

            }
            else if (radImageControl_Part_Delete.Checked == true)
            {

            }
        }
        public void ArraysChanged()
        {
            //This is not related to the original question, but if someone knows how to fill a 
            //Combo-box with the list it would save me quite some time
            cmbImageControl_Color.Items
        }

下面是我试图搜索文本的地方,因为在这种情况下简单地输入 ImgColor.Contains("text") 是行不通的,我已经清空了有问题的 if 语句。

        public bool ErrorHandling(string Type)
        {
            if (Type == "control")
            {
                if (txtImageControl_Name.Text == "")
                {
                    MessageBox.Show("Name must not be blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return true;
                }
                else if ()
                    //Want to check if ImgBase contains a name equal to the text in txtImageControl_Name.Text
                {
                    MessageBox.Show("Name cannot be identical to a name already in place", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return true;
                }
                else if ()
                    // Want to check if ImgColor contains a name equal to the text in txtImageControl_Name.Text
                {
                    MessageBox.Show("Name cannot be identical to a name already in place", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return true;
                }
            }
            return false;
        }

为了便于阅读,已对不相关的代码进行了编辑。

【问题讨论】:

标签: c# list constructor contains


【解决方案1】:

也许这就是你要找的东西

if (ImgBase.Any(x => x.Name == txtImageControl_Name.Text))
   ...
else if (ImgColor.Any(x => x.Name == txtImageControl_Name.Text))
   ...

【讨论】:

  • 效果很好,谢谢,您介意解释一下它是如何工作的吗?
  • @KawaiiDouchebag 它只是枚举一个集合并在 lambda 表达式上返回 true 或 false,在这种情况下,是集合中任何元素的名称,等于控件的文本
  • 我看到在 x.name 期间 x 被用作某种形式的通配符,但是 x => 部分是做什么用的?
  • "enumerating" 是指迭代集合中的每个项目,而不是"enumeration type"
  • 是的,抱歉,我注意到为时已晚,并编辑了我对相关部分的评论
猜你喜欢
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 2020-01-10
  • 2020-03-21
  • 2013-05-11
  • 1970-01-01
  • 2015-11-26
  • 2023-03-22
相关资源
最近更新 更多