1

I have to method in model of my asp.net mvc project.

public JsonResult GetProductsByDepList(string depID)
{
  JsonResult jr = new JsonResult();
  var _product = from a in DataContext.GetProductsByDep(Convert.ToInt32(depID))
  select new { ID = a.ID, ProName = a.Name };
  jr.Data = _product.ToList();
  jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
  return jr;
 }


 public JsonResult GetProductByCatList(string depID, string catID)
 {
   JsonResult jr = new JsonResult();
   var _product = from a in   
   DataContext.GetProductsByCat(Convert.ToInt32(depID),Convert.ToInt32(catID))
   select new { ID = a.ID, ProName = a.Name };
   jr.Data = _product.ToList();
   jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
   return jr;
 }

I want to combine these 2 method, it is work the same each other, except the parameter of the function.

Any ideas please.

1
  • 1
    Given that these two methods do different things, why do you want to combine them? Commented Apr 10, 2012 at 9:32

5 Answers 5

3

Use an optional argument:

public JsonResult GetProductByCatList(string depID, string catID = "0")
Sign up to request clarification or add additional context in comments.

Comments

2
 public JsonResult GetProductByCatList(string depID, string catID = "-1")
 {
   //common shared code
   return jr;
 }

Assuming that default value is -1 for catID

Comments

2

You could try:

public JsonResult GetProductByCatList(string depID, string catID = null)
{
  JsonResult jr = new JsonResult();
  if (String.IsNullOrEmpty(catID))
  {
      var _product = from a in DataContext.GetProductsByDep(Convert.ToInt32(depID))  
      select new { ID = a.ID, ProName = a.Name };
      jr.Data = _product.ToList();      
  } else {
      var _product = from a in   
      DataContext.GetProductsByCat(Convert.ToInt32(depID),Convert.ToInt32(catID))
      select new { ID = a.ID, ProName = a.Name };
      jr.Data = _product.ToList();      
  }
  return jr;
}

Comments

2

You can try like this:

   public JsonResult GetProductsByDepList(string depID)
   {
       return GetProductByCatList(depID, null);
   }

   public JsonResult GetProductByCatList(string depID, string catID)
   {
       JsonResult jr = new JsonResult();
       var _product = null;
       if (!string.IsNullOrEmpty(catID))
       {
           _product = from a in   
               DataContext.GetProductsByCat(Convert.ToInt32(depID),Convert.ToInt32(catID))
               select new { ID = a.ID, ProName = a.Name };
       }
       else
       {
           _product = from a in DataContext.GetProductsByDep(Convert.ToInt32(depID))
               select new { ID = a.ID, ProName = a.Name };
       } 

       jr.Data = _product.ToList();
       jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
       return jr;
   }

2 Comments

this has errors, you can't declare _product twice and you can't just put the select bit off on it's own after the if/else statements. Also you need a return in the top function.
But otherwise this has potential :)
1

Default parameters are available from 4.0 version. Also, read this article from MS http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85556.aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.