我正在使用树控件,要求将数据展平后再将其交给控件。我已经尝试了各种在线示例,但无法完全“平整”数据。
类似代码的样子:
我猜我在这儿走错了路...
public IQueryable<DeviceHierarchyDataItem> Query(int circuitConnectId)
{
var devices = (from device in UnitOfWork.Device
join deviceCircuit in UnitOfWork.DeviceCircuit on device.Id equals deviceCircuit.DeviceId
where
deviceCircuit.CircuitConnectId == circuitConnectId
select device)
.Include( x=> x.Meters);
// Not sure what to do NEXT (previous attempts have failed)
}
类:
public class DeviceHierarchyDataItem
{
public int Id { get; set; } //<-- the 'Id' of the Device or Meter
public int? ChildOf { get; set; } //<-- the 'Id' of the PARENT Device or NULL
public int CircuitConnectId { get; set; }
public string DisplayName { get; set; } //<-- the 'Name' of the Device or Meter
}
public class Device
{
public int Id { get; set; }
public string DeviceName { get; set; }
public List<Meter> Meters { get; set; }
}
public class Meter
{
public int Id { get; set; }
public string MeterName { get; set; }
}
答案 0 :(得分:2)
您可以使用SelectMany展平集合的集合。
var devices = (from device in UnitOfWork.Device
join deviceCircuit in UnitOfWork.DeviceCircuit on device.Id equals deviceCircuit.DeviceId
where
deviceCircuit.CircuitConnectId == circuitConnectId
select device)
.Include( x=> x.Meters);
var deviceHierarchyDataItems = devices
.SelectMany(device => device.Meters.Select(meter => new DeviceHierarchyDataItem
{
Id = meter.Id,
ChildOf = device.Id,
CircuitConnectId = 0, // don't know what you need here
DisplayName = meter.MeterName
}))
.Concat(devices.Select(device => new DeviceHierarchyDataItem
{
Id = device.Id,
ChildOf = null,
CircuitConnectId = 0, // don't know what you need here
DisplayName = device.DeviceName
}));
或者:
var deviceHierarchyDataItems = devices
.SelectMany(device => device.Meters.Select(meter => new DeviceHierarchyDataItem
{
Id = meter.Id,
ChildOf = device.Id,
CircuitConnectId = 0, // don't know what you need here
DisplayName = meter.MeterName
}).Concat(new List<DeviceHierarchyDataItem>()
{
new DeviceHierarchyDataItem
{
Id = device.Id,
ChildOf = null,
CircuitConnectId = 0, // don't know what you need here
DisplayName = device.DeviceName
}
}));
答案 1 :(得分:1)
Dynamic Data是用于处理列表的绝佳软件包。
使用反应式树功能展平列表。找到示例here。
答案 2 :(得分:1)
尝试以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication86
{
class Program
{
static void Main(string[] args)
{
int circuitConnectId = 123;
var devices = (from device in UnitOfWork.Device
join deviceCircuit in UnitOfWork.DeviceCircuit on device.Id equals deviceCircuit.Id
where
deviceCircuit.CircuitConnectId == circuitConnectId
select new { device = device, deviceCircuit = deviceCircuit })
.Select(x => new
{
meters = x.device.Meters.Select(y => new { deviceName = x.device.DeviceName, deviceId = x.device.Id, meterId = y.Id, meterName = y.MeterName, circuitId = x.deviceCircuit.Id, circuitDisplayName = x.deviceCircuit.DisplayName }),
}).ToList();
}
}
public class UnitOfWork
{
public static List<Device> Device { get; set; }
public static List<DeviceHierarchyDataItem> DeviceCircuit { get; set; }
}
public class DeviceHierarchyDataItem
{
public int Id { get; set; } //<-- the 'Id' of the Device or Meter
public int? ChildOf { get; set; } //<-- the 'Id' of the PARENT Device or NULL
public int CircuitConnectId { get; set; }
public string DisplayName { get; set; } //<-- the 'Name' of the Device or Meter
}
public class Device
{
public int Id { get; set; }
public string DeviceName { get; set; }
public List<Meter> Meters { get; set; }
}
public class Meter
{
public int Id { get; set; }
public string MeterName { get; set; }
}
}