2016-08-02 79 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