2016-06-08 1 views
1
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)); 
     } 
    } 
} 

Si je tente de mettre les deux listes de treedata aveclist.AddRange ne peut pas convertir implicitement le type « vide »

return Location.AddRange(Meters)); 

Je reçois l'erreur suivante: Impossible de convertir implicitement le type « vide » à System.Collections.Generic.List

Je sais que le type de retour de the.AddRange est vide (null) mais comment puis-je mettre les deux liste ensemble?

Répondre

6

List.AddRange ne renvoie rien car il modifie directement la liste:

Location.AddRange(Meters); 
return Location; 

Si vous ne voulez pas modifier, vous pouvez utiliser LINQ:

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

Mais je n » t créer les deux autres listes, c'était plus efficace:

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(); 
} 
+0

Cela a fonctionné merci! – user2811133