Linq 和列表列表
Linq and List of Lists
重要提示:我想使用 Linq 执行此操作。
我有一个设备列表,其中有一个选项列表。假设有 5 个设备,每个设备都有几个选项。
现在我需要获取选项名称包含 "Foot Pedal" 且其安装设置为 "true" 的所有设备。
谢谢
代码如下:
public class Device
{
public Int32 ID { get; set; }
public String Name { get; set; }
public List<Option> Options { get; set; }
}
public class Option
{
public Int32 ID { get; set; }
public String Name { get; set; }
public Boolean Installed { get; set; }
}
public class DeviceTest
{
public DeviceTest()
{
List<Device> devices = new List<Device>();
Option option;
Device device;
device = new Device();
device.ID = 1;
device.Name = "WC-1";
devices.Add(device);
device.Options = new List<Option>();
option = new Option();
option.ID = 1001;
option.Name = "Foot pedal";
option.Installed = true;
device.Options.Add(option);
option = new Option();
option.ID = 1002;
option.Name = "Wheel";
option.Installed = true;
device.Options.Add(option);
device = new Device();
device.ID = 2;
device.Name = "WC-2";
devices.Add(device);
device.Options = new List<Option>();
option = new Option();
option.ID = 1003;
option.Name = "Foot pedal";
option.Installed = true;
device.Options.Add(option);
option = new Option();
option.ID = 1004;
option.Name = "Wheel";
option.Installed = true;
device.Options.Add(option);
device = new Device();
device.ID = 3;
device.Name = "WC-3";
devices.Add(device);
device.Options = new List<Option>();
option = new Option();
option.ID = 1005;
option.Name = "Foot pedal";
option.Installed = false;
device.Options.Add(option);
option = new Option();
option.ID = 1006;
option.Name = "Wheel";
option.Installed = true;
device.Options.Add(option);
device = new Device();
device.ID = 4;
device.Name = "WC-4";
devices.Add(device);
device.Options = new List<Option>();
option = new Option();
option.ID = 1007;
option.Name = "Foot pedal";
option.Installed = false;
device.Options.Add(option);
option = new Option();
option.ID = 1008;
option.Name = "Wheel";
option.Installed = false;
device.Options.Add(option);
}
}
var targetDevices = devices.Where(i => i.Options.Any(j => j.Installed && j.Name == "Foot pedal")).ToList()
或
var targetDevice =
(from device in devices
from option in device.Options
where option.Installed && option.Name == "Foot pedal"
select device).ToList()
注意最后的 ToList(),如果结果为 null,这将 return 一个空列表而不是 null。
这是一个非常简单的查询:
allDevices.Where(d => d.Options.Any(o => o.Name == "Foot Pedal" && o.Installed));
请记住,lambda 只是一个函数。它可以调用其他函数、声明变量以及您可以在普通代码中执行的所有其他操作。
重要提示:我想使用 Linq 执行此操作。
我有一个设备列表,其中有一个选项列表。假设有 5 个设备,每个设备都有几个选项。
现在我需要获取选项名称包含 "Foot Pedal" 且其安装设置为 "true" 的所有设备。
谢谢
代码如下:
public class Device
{
public Int32 ID { get; set; }
public String Name { get; set; }
public List<Option> Options { get; set; }
}
public class Option
{
public Int32 ID { get; set; }
public String Name { get; set; }
public Boolean Installed { get; set; }
}
public class DeviceTest
{
public DeviceTest()
{
List<Device> devices = new List<Device>();
Option option;
Device device;
device = new Device();
device.ID = 1;
device.Name = "WC-1";
devices.Add(device);
device.Options = new List<Option>();
option = new Option();
option.ID = 1001;
option.Name = "Foot pedal";
option.Installed = true;
device.Options.Add(option);
option = new Option();
option.ID = 1002;
option.Name = "Wheel";
option.Installed = true;
device.Options.Add(option);
device = new Device();
device.ID = 2;
device.Name = "WC-2";
devices.Add(device);
device.Options = new List<Option>();
option = new Option();
option.ID = 1003;
option.Name = "Foot pedal";
option.Installed = true;
device.Options.Add(option);
option = new Option();
option.ID = 1004;
option.Name = "Wheel";
option.Installed = true;
device.Options.Add(option);
device = new Device();
device.ID = 3;
device.Name = "WC-3";
devices.Add(device);
device.Options = new List<Option>();
option = new Option();
option.ID = 1005;
option.Name = "Foot pedal";
option.Installed = false;
device.Options.Add(option);
option = new Option();
option.ID = 1006;
option.Name = "Wheel";
option.Installed = true;
device.Options.Add(option);
device = new Device();
device.ID = 4;
device.Name = "WC-4";
devices.Add(device);
device.Options = new List<Option>();
option = new Option();
option.ID = 1007;
option.Name = "Foot pedal";
option.Installed = false;
device.Options.Add(option);
option = new Option();
option.ID = 1008;
option.Name = "Wheel";
option.Installed = false;
device.Options.Add(option);
}
}
var targetDevices = devices.Where(i => i.Options.Any(j => j.Installed && j.Name == "Foot pedal")).ToList()
或
var targetDevice =
(from device in devices
from option in device.Options
where option.Installed && option.Name == "Foot pedal"
select device).ToList()
注意最后的 ToList(),如果结果为 null,这将 return 一个空列表而不是 null。
这是一个非常简单的查询:
allDevices.Where(d => d.Options.Any(o => o.Name == "Foot Pedal" && o.Installed));
请记住,lambda 只是一个函数。它可以调用其他函数、声明变量以及您可以在普通代码中执行的所有其他操作。