首页 > 解决方案 > 如何在不搜索相同字符串的情况下比较两个列表?

问题描述

我在 C# 解决方案中有两个列表:一个用于工厂的任务,另一个用于执行这些任务的机器。当要在工位 1 执行任务时,必须显示位于工位 1 且能够执行任务的所有机器。任务和机器在它们各自的“任务”和“机器”类中声明。

我开始意识到“foreach”循环不能识别其他列表中的项目。我不知道如何根据另一个列表中的条件在一个列表中开始查询。

“机器”类

class Machine
{
    public string machineName { get; set; }
    public int machineID { get; set; }
    public int Station { get; set; }
    public Function func{ get; set; }

    public Machine(string name = "No Name", int machineid=0, 
    int stat = 0, string functionname = "No Function", bool contact = false)
    {
        machineName = name;
        machineID = machineid;
        Station = stat;
        func = new Function (functionname, contact);
    }
}

“任务”类

class Task
{
    public string taskName { get; set; }
    public int Station { get; set; }

    public process(string name = "No Name", int stat = 0)
    {
        processName = name;
        Station = stat;
    }
}

主要的

List<Task> taskList = new List<Task>();
var station1list = taskList.Where(m => m.Station==1).ToList();
foreach (var g in station1list)
{
    Console.WriteLine("\nThe tasks at station 1: {0}", 
    g.taskName);              
}

预期结果:如果要在工位 1 执行任务 A,则程序应在工位 1 搜索具有相关功能的机器,例如“执行任务 A”。必须对所有站点的所有任务重复此操作。

标签: c#classnestedattributes

解决方案


//Assuming you have a global machines list as well, say,

machineGlobalList

var station1list = taskList.Where(m => m.Station==1).ToList();
foreach (var g in station1list)
{

     //get machines at station one
     var machinesAtStation = machineGlobalList.Where(x => x.Station == g.Station).ToList();

     //Call their function...
     machinesAtStation.ForEach(x => x.func = new Function(para1,para2))//pass the respective parameters

 }

推荐阅读