list.AddRange 无法隐式转换类型为 'void'

5
   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));
        }
    }
}

如果我尝试用

将两个树数据列表组合在一起
return Location.AddRange(Meters));

我遇到了以下错误: 无法将类型“void”隐式转换为System.Collections.Generic.List 我知道AddRange的返回类型是void(空值), 但我如何将这两个列表合并在一起?
2个回答

17

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.AddRange直接修改列表,因此不返回任何内容:谢谢!我无法理解这个错误消息。 - Brian White

0

我遇到了这个问题,因为再次设置结果

result = result.AddRange(listTestReaders); // wrong

但我应该仅使用AddRange方法,而不是与=赋值

result.AddRange(listTestReaders); // correct

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接