2016-08-02 4 views
0
Public JsonResult GetDetails() 
{ 
    List<Cust> Customers = new List<Cust>(); 
    Customers = GetCustomerDetails(); 
    var name = Customers.Select(e => new{e.custname}).Distinct().ToList(); 
    var dept = Customers.Select(e => new{e.deptname}).Distinct().ToList(); 
    var response = new{CustomerNames = name, CustomerDepartments = dept}; 
    return Json(response, JsonRequestBehaviour.AllowGet(); 
} 

위 메서드는 json 객체를 반환하지만 이제는이 메서드의 반환 값과 함께이 응답의 하위 집합을 반환해야합니다. 필터를 사용할 수 있습니까? 부서 유형별로 동일한 메소드에서 두 개의 jon 객체를 반환합니다.두 개의 json 객체를 반환하는 컨트롤러의 메서드

답변

2

확실히. 익명 객체에 속성을 하나 더 추가하여 반환 할 수 있습니다.

public JsonResult GetDetails() 
{ 
    var customers = GetCustomerDetails(); 
    var names = customers.Select(e => new {e.custname}).Distinct().ToList(); 
    var depts = customers.Select(e => new { e.deptname}).Distinct().ToList(); 
    var deptSubSet = depts.Where(f=>f.deptname=="IT").ToList(); 
    //replace this with your condition 

    var response = new { CustomerNames = names, 
         CustomerDepartments = depts, 
         FilteredDepartments = deptSubSet 
        }; 
    return Json(response, JsonRequestBehaviour.AllowGet(); 
} 

절은 당신이 서브 세트를 얻기 위해 사용할 필요가 어디에서 무엇과 Where 조건 LINQ 코드를 교체합니다.

BTW, 결과는 배열이 아니며 3 개의 속성이있는 개체가됩니다. 이러한 속성의 값은 배열입니다.

+0

답장을 보내 주셔서 감사드립니다 ... 부서 이름이 IT이고 이름이 'N'으로 시작하는 모든 고객을 선택할 수 있습니까? 두 가지 결과를 병합 할 수 있습니까? – Agasthya

+0

예. [StartsWith] (http://stackoverflow.com/questions/34777044/linq-query-using-contains-is-not-working/34777091#34777091) 확장 방법 – Shyju

+0

을 사용할 수 있지만 어떻게 이름과 부서를 병합 할 수 있습니까? 위의 응답 객체의 속성에 저장합니다. – Agasthya

관련 문제