2016-08-02 65 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子句使用來獲得子集Where條件LINQ代碼。

順便說一句,結果不會是一個數組,它將是一個具有3個屬性的對象。這些屬性的值將是一個數組。

+0

非常感謝您的回覆......是否可以選擇部門名稱爲IT,名稱以'N'開頭的所有客戶,我們可以合併兩個結果。 – Agasthya

+0

是的。你可以使用[StartsWith](http://stackoverflow.com/questions/34777044/linq-query-using-contains-is-not-working/34777091#34777091)擴展方法 – Shyju

+0

,但我怎樣才能合併名稱和部門和將其存儲在上述響應對象的屬性中。 – Agasthya

相關問題