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

을 변환 할 수 없습니다 나는 다음과 같은 오류 얻을 : 암시 적으로 '무효'형식을 변환 할 수 없습니다를 System.Collections.Generic.Listlist.AddRange 암시 적 유형 '무효'

반환 유형을 알 수 있습니다 .AddRange void (null) 하지만 두 목록을 함께 넣을 수 있습니까?

답변

6

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

감사합니다. – user2811133