【发布时间】:2022-01-01 04:03:00
【问题描述】:
我正在尝试从Device 表中获取针对当前客户的设备列表。我做到了,但使用了 foreach 循环,但我想使用 linq 来进行操作。
这是我的班级结构
public class Device
{
public string type { get; set; }
public int id { get; set; }
public List<Role> roles { get; set; }
}
public class Role
{
public int roleId { get; set; }
public string roleName { get; set; }
public int customerId { get; set; }
}
这是我的代码
var devList = list.Where(x => x.type == "device1").ToList();
foreach (var item in devList)
{
if (item.roles != null)
{
var kss = item.roles.Where(z => z.customerId == kunId).FirstOrDefault();
if (kss != null)
{
m.devicesList.Add(item);
}
}
}
我需要针对当前客户获取设备,因此我必须将我当前的用户 ID 与 customerId 进行比较。
如何将其转换为 linq-to-sql?
【问题讨论】: