list.AddRange Cannot implicitly convert type 'void'
Asked Answered
M

2

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

If i try to put the two lists of TreeData together with

return Location.AddRange(Meters));

I get the following error: Cannot implicitly convert type 'void' to System.Collections.Generic.List

I know the return type of the.AddRange is void(null) but how can i put the two list together ?

Mnemonic answered 8/6, 2016 at 8:30 Comment(0)
T
17

List.AddRange doesn't return anything since it modifies the list directly:

Location.AddRange(Meters);
return Location;

If you don't want to modify it you can use LINQ:

return Location.Concat(Meters).ToList();

But then i wouldn't create the other two lists, this was more efficient:

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();
}
Thisbee answered 8/6, 2016 at 8:33 Comment(1)
List.AddRange doesn't return anything since it modifies the list directly: Thank you! I couldn't figure out this error messageRecommendatory
S
0

I had this problem because set the result again:

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

But I should use AddRange method only without assign with =

result.AddRange(listTestReaders); // correct
Subconscious answered 8/1, 2023 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.