合并两个类值并使用.net返回

时间:2017-05-25 11:58:07

标签: c# asp.net-mvc asp.net-web-api

我使用ajax调用了Api控制器。我编写了如下的Api控制器代码,

[HttpPost]
[Route("api/ajaxapi/caselistmethod")]
public List<CaseValues> AjaxCaseListMethod()
{
     CaseModel users = new CaseModel();
     string userName = HttpContext.Current.User.Identity.Name;
     List<CaseValues> caseList = new List<CaseValues>();
     caseList = users.GetCasedetails(userName);
     //caseList = caseList.Take(10).ToList();
     return caseList;
}

并在类中编写GetCasedetails方法,如下所示

public class CaseModel
{
     public List<CaseValues> CaseDetails { get; set; }

     public List<CaseValues> GetCasedetails(string userName)
     { 
           RequiredValues values = new RequiredValues();
           DropDownListvalues requiredList = new DropDownListvalues();
           requiredList = setDropDownListValues("exec sp_Search_GetHISA");
           using (var cases = new Entities())
           {
                System.Guid userId = (from list in cases.aspnet_Membership
                                          where list.Email == userName
                                          select list.UserId).FirstOrDefault();


                values.UserType= "Admin";

                string coder = (from list in cases.Accidents
                                    join users in cases.aspnet_Users on userId equals users.UserId
                                    where userName == list.coder
                                    select list.coder).FirstOrDefault();

                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("UserId", userId));

                CaseDetails = cases.Database.SqlQuery<CaseValues>("exec sp_CaseSearch @UserId", sqlParams.ToArray()).ToList();

            }

            return CaseDetails;
   }

   public class CaseValues
   {
         public int caseid { get; set; }
         public int city { get; set; }
         public int caseNum { get; set; }
   }

我可以使用此方法返回值。但我必须使用如下的usertype创建另一个类,

 public class RequiredValues
 {
       public string UserType { get; set; }
 }

我需要返回旧的类列表值和新的类列表值。请提出你的建议。

1 个答案:

答案 0 :(得分:1)

您可以编辑RequiredValues类,如下所示:

public class RequiredValues
{
   public string UserType { get; set; }
   public List<CaseValues> CaseDetails { get; set; }
}

结束编辑方法如下:`

public List<RequiredValues> GetCasedetails(string userName)
 { 
       RequiredValues values = new RequiredValues();
       DropDownListvalues requiredList = new DropDownListvalues();
       requiredList = setDropDownListValues("exec sp_Search_GetHISA");
       using (var cases = new Entities())
       {
            System.Guid userId = (from list in cases.aspnet_Membership
                                      where list.Email == userName
                                      select list.UserId).FirstOrDefault();


            values.UserType= "Admin";

            string coder = (from list in cases.Accidents
                                join users in cases.aspnet_Users on userId equals users.UserId
                                where userName == list.coder
                                select list.coder).FirstOrDefault();

            List<SqlParameter> sqlParams = new List<SqlParameter>();
            sqlParams.Add(new SqlParameter("UserId", userId));

            values.CaseDetails = cases.Database.SqlQuery<CaseValues>("exec sp_CaseSearch @UserId", sqlParams.ToArray()).ToList();

        }

        return values;

}

希望这有帮助!