c# - 如何在不搜索相同字符串的情况下比较两个列表?
问题描述
我在 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”。必须对所有站点的所有任务重复此操作。
解决方案
//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
}
推荐阅读
- javascript - 将事件动态添加到 div
- java - 如何在 Java 的标准类中设置通用对象?
- html - Django 2.0:后退按钮根据您来自哪里重定向到不同页面
- google-apps-script - 谷歌表 谷歌脚本中的特殊公式防止复制
- textwrangler - TextWrangler 和 BBEdit 多文件搜索无法根据文件名进行过滤
- javascript - 如何使用推送侧菜单防止正文滚动
- dask - 在自适应模式下使用 Dask 时分配给一名工作人员的所有任务
- ios - 无法让 UIButton 在 Swift 4 中调用代码块
- macos - OS X 后台获取等效项
- css - 您如何更改 wordpress 中某个特定链接的链接颜色?