简体   繁体   English

如何从列表中获取项目并将其与WPF C#中另一个列表中的另一个项目进行匹配

[英]How to take an item from a list and match it with another item in another list in wpf c#

I have 2 lists in my class which both contain strings, I am using winforms : 我班上有两个列表,两个列表都包含字符串,我正在使用winforms

public List<Journey> journeys = new List<Journey>();
public List<Vehicle> vehicles = new List<Vehicle>();

I want to be able to take eg the first element in the list journeys and the first element in the list vehicles , combine the data and then output that data to a listbox called ShowAll . 我希望能够采用例如列表journeys的第一个元素和列表vehicles的第一个元素,合并数据,然后将数据输出到名为ShowAll的列表框。

I want to be able to do this with every item in the lists, so 2nd element in journeys matches with 2nd element in vehicles and 3rd element in journeys matches with 3rd element in vehicles etc. 我希望能够对列表中的每个项目执行此操作,因此journeys中的第二个元素与vehicles第二个元素匹配, journeys中的第三个元素与vehicles第三个元素等匹配。

I don't have a clue how to do this but it would be great if anyone could give me a hand. 我不知道如何执行此操作,但是如果有人可以帮我,那将是很棒的。 Thanks! 谢谢!

Try something like 尝试类似

class CompositeObject //use a better name that fits your context..
{
  Journey journey; public Journey Property { get; private set; }
  Vehicle vehicle; public Vehicle Property { get; private set; }

  public CompositeObject(Journey journey, Vehicle vehicle)
  {
    this.journey = journey;
    this.vehicle = vehicle;
  }

  public override string ToString()
  {
    return this.vehicle.ToString() + " | " + this.journey.ToString();
  }
}

now you can refactor the ToString to give you output suitable for your gui (the listbox). 现在,您可以重构ToString以提供适合您的gui(列表框)的输出。 and when it comes to writing into the listbox a simple for-loop should do 当涉及到写入列表框时,应该使用简单的for循环

I didn't test this. 我没有测试。 I just wrote it here but it should work. 我只是在这里写的,但是应该可以。 Give it a shot. 试一试。

Good Luck. 祝好运。

StringBuilder sb = new StringBuilder();

for(int n=0; n< journeys.Length; journeys++)
    sb.Append(journeys[n]).Append(vehicles[n]).Append("\n");

ShowAll.Clear();
ShowAll.Text = sb.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM