namespace SubscriptionWebsite.PlaceHolders
{
public class TreeData
{
public int ID { get; set; }
public int? ParentLocationID { get; set; }
public string name { get; set; }
public int? Locationlevel { get; set; }
public string participation { get; set; }
}
}
namespace SubscriptionWebsite.Managers
{
public class TreeDataManager
{
SubscriptionWebsite.Entities.acc_core_dbEntities db = new SubscriptionWebsite.Entities.acc_core_dbEntities();
public List<TreeData> GetTreeData()
{
List<TreeData> Location = db.frm_location.Where(x => !x.Removed && x.Revision == 0).Select(loc => new TreeData
{
ID = loc.ID,
ParentLocationID = loc.ParentLocationID,
name = loc.frm_location_level.SystemId.Equals(5) ? loc.frm_location_address.FirstOrDefault(c => !c.Removed && c.Revision == 0).Street1 : loc.Name,
Locationlevel = loc.frm_location_level.SystemId,
participation = loc.IsActive ? "Yes" : "No",
}).ToList();
List<TreeData> Meters = db.frm_connection_meter.Where(x => !x.Removed && x.Revision == 0).Select(l => new TreeData
{
Locationlevel = 6,
ID = l.ID,
ParentLocationID = l.frm_location.ID,
name = l.MeterNumber,
participation = l.IsMain ? "Yes" : "No",// change to IsActive after db update
}).ToList();
return Location.AddRange(Meters));
}
}
}
如果我尝试将TreeData的两个列表与
return Location.AddRange(Meters));
我收到以下错误:
无法将类型'void'隐式转换为System.Collections.Generic.List
我知道the.AddRange的返回类型是void(null)
但是我怎么把两个清单放在一起呢?
参考方案
List.AddRange
不返回任何内容,因为它直接修改了列表:
Location.AddRange(Meters);
return Location;
如果您不想修改它,可以使用LINQ:
return Location.Concat(Meters).ToList();
但是后来我不创建其他两个列表,这样效率更高:
public List<TreeData> GetTreeData()
{
var locations = db.frm_location
.Where(x => !x.Removed && x.Revision == 0)
.Select(loc => new TreeData
{
ID = loc.ID,
ParentLocationID = loc.ParentLocationID,
name = loc.frm_location_level.SystemId.Equals(5) ? loc.frm_location_address.FirstOrDefault(c => !c.Removed && c.Revision == 0).Street1 : loc.Name,
Locationlevel = loc.frm_location_level.SystemId,
participation = loc.IsActive ? "Yes" : "No",
});
var meters = db.frm_connection_meter
.Where(x => !x.Removed && x.Revision == 0)
.Select(l => new TreeData
{
Locationlevel = 6,
ID = l.ID,
ParentLocationID = l.frm_location.ID,
name = l.MeterNumber,
participation = l.IsMain ? "Yes" : "No",// change to IsActive after db update
});
return locations.Concat(meters).ToList();
}
按子列表的值对LIst列表进行排序 - javaprivate List<String> subList; private List<List<String>> records = new ArrayList<List<String>>(); for(....){ subList = new ArrayList<String>(); …
使用2个字段对C#进行列表排序 - c#我有一个自定义对象的列表。该对象包含1个字符串和2个小数。我想根据第二个十进制字段降序排序,然后按第一个十进制字段排序。例如:object 1 -> "a", 100, 10 object 2 -> "b", 300, 0 object 3 -> "c", 200, 200 obj…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
如何克隆或复制列表? - python在Python中克隆或复制列表有哪些选项?使用new_list = my_list时,对new_list的任何修改每次都会更改my_list。为什么是这样? python参考方案 使用new_list = my_list,实际上没有两个列表。分配只是将引用复制到列表,而不是实际列表,因此new_list和my_list都在分配后引用同一列表。要实际复制列表,…
与在Java中分配数组与分配链表相比,要使用多少内存? - java我的猜测是,存储在数组中的每个值都有32位/ 64位字(取决于CPU)。因此它将是数组大小X 32位/ 64位。对于链表,存储指向下一个元素的引用将是链接列表的两倍。因此它将是2 *数组大小X 32位/ 64位。这是正确的,我有什么遗漏吗? java大神给出的解决方案 多得多。链表中的每个元素都有:指向下一个元素的指针,指向上一个元素的指针,指向项目值的指针…