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

如果我試圖把TreeData的兩個清單list.AddRange無法隱式轉換類型「無效」

return Location.AddRange(Meters)); 

在一起,我收到以下錯誤: 無法隱式轉換類型「無效」到System.Collections.Generic.List

我知道the.AddRange的返回類型是void(null) 但我怎麼能把這兩個列表放在一起?

回答

6

List.AddRange因爲它修改直接在列表不返回任何內容:

Location.AddRange(Meters); 
return Location; 

如果你不想修改它,你可以使用LINQ:

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

但後來我纔不會」 t創建另外兩個列表,這是更有效的:

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

這工作謝謝你! – user2811133

相關問題