查询存在多个同名元素的List

Query where multiple elements of same name exist List

Windows 表单应用

好的抱歉我有一个class

public class comm
   {

      private int dayV;
      private string timeV;
      private decimal priceV;
      private string nameValue;
      private string descriptionV;

      public comm(string Day, string Time, string Price, string Name, string Description)
      {
         try
         {
            this.dayV = Convert.ToInt32(Day);
         }
         catch (Exception ex)
         {

         }

         this.timeV = Time;
         decimal PriceVal;
         var irishCulture = CultureInfo.CreateSpecificCulture("en-IE");
         if (decimal.TryParse(Price, NumberStyles.Currency, irishCulture, out PriceVal))
         {
            this.priceV = Convert.ToDecimal(PriceVal);
         }
         this.nameValue = Name;
         this.descriptionV = Description;
      }


      public int GetDay() { return dayV; }
      public string GetTime() { return timeV; }
      public string GetPrice() { return (Convert.ToString(priceV)); }
      public string GetName() { return nameValue; }

      // Returns full event Description
      public string Description()
      {
         string price = "Price: ";
         return timeV + "\r\n" + price + "€" + priceV + "\r\n" + this.descriptionV;
      }
   }

我将文本文件读入一个列表,并将每个字段分配给相应的class实例变量。

文本文件看起来像这样

1,02:29 pm,€55.00,John D,Main st 
1,05:43 pm,€70.00,John D,Bridge st
2,01:43 pm,€100.00,Mike O,First Floor A12
1,10:44 am,€100.00,Colm Collins,First Floor A13
2,11:44 am,€110.00,Martin O, Exp 01

读取文件的方法。

    public List<communityEvent> CreateList()
          {
             List<comm> entries = new List<comm>();
             try
             {
                StreamReader fileIn = new StreamReader(path);
                //Read the file
                while (!fileIn.EndOfStream)
                {
                   String line = fileIn.ReadLine();
                   String[] pieces = line.Split(',');
                   if (pieces.Length == 5)
                   {
                      comm Eve = new comm(pieces[0], pieces[1], pieces[2], pieces[3], pieces[4]);
                      entries.Add(Eve);
                   }
        return entries;

在主程序中

    private List<comm> various;



    private void ExtractData(string eventComboBox)
{
    var query = from v in various
                     where vComboBox == v.GetName()
                     select v.Description();

         foreach (var vr in query)
         {
            descriptionTextBox.Text = vr;
         }
}

因此组合框内容将如下所示:-

             John D
             John D
             Colm Collins
             Martin O

我想要的是,每次用户 select 都是不同的名称。 我得到相应的 description() 例如。 selected 时第一个 John D 将使用 Description() 填充描述文本框,Description() 应该是该行中的字段。 我遇到的问题是,有些名字与描述相同 returns 第一个描述()对于所有具有相同名字的人,无论我 select.

感谢您提供的补充信息。它仍然很模糊——我不知道 "the individual description" 是什么意思,因为你写了,而且你的新数据示例显示,数据中可以有多个匹配项。当你select一个名字匹配多个名字时,没有“个人描述”。

就是说,如果您只想要一个描述,那么您真正想要的似乎是将 ComboBox 中的 单个 项目与 您的数据中的单个 项目ComboBox 是从中填充的。

很遗憾,您仍未按要求提供完整的代码示例。所以没有办法知道 ComboBox 是如何填充的,甚至 various 变量是如何声明的。但是你说数据被读取"into a List",所以我们假设:

  • 您有某种类型 DataRow 表示文件中的一行文本
  • various 声明为 List<DataRow>
  • ComboBox 中的项目顺序与 various 列表中的项目顺序完全相同

如果以上都是正确的(并且没有完整的代码示例,任何阅读您问题的人都不可能确定它是正确的),那么您根本不需要或不想查询数据。 您已经知道select编辑的项目的确切索引!

让我们进一步假设:

  • 您的 ComboBox 的名称是 "comboBox1",您的代码中有一个名为 comboBox1 的字段,它引用了 ComboBox 实例

在这种情况下,以下代码足以替代您提供的整个代码示例:

descriptionTextBox.Text = various[comboBox1.SelectedIndex].Description();

这会检索ComboBox中当前selection的索引,然后使用该索引(因为ComboBox中的数据顺序应该完全相同如 List<DataRow>) 从 List<DataRow> 中检索 DataRow 值,目的是调用 Description() 方法来获取该行数据的实际描述并分配它到你的 descriptionTextBoxText 属性.

如果您的 ComboBox 有不同的名称,只需使用正确的名称代替上面的 comboBox1

同样,如果这不能解决您的问题,请在问题中添加详细信息。提供一个最小的完整代码示例,并准确解释它的作用以及它与您想要的有何不同。

试试这个,

descriptionTextBox.Text = various[comboBox.SelectedIndex].Description();

感谢大家的帮助,以下是对我有用的 我创建了一个方法并将组合框文本和索引输入其中。

    private void ExtractData(string eventComboBo, int index)
  {

     var query = from v in various
                 where eventComboBo == v.GetName() && eventComboBox.SelectedIndex.Equals(index)
                 select v.Description();

     foreach (var ev in query)
     {
        descriptionTextBox.Text = ev;
     }