作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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
我知道.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();
}
关于c# - list.AddRange 无法隐式转换类型 'void',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37697170/
我是一名优秀的程序员,十分优秀!