Declare Anonymous Type.

Posted by Sheetal.Swamy under Regular Expressions on 7/19/2011 | Points: 10 | Views : 1738 | Status : [Member] | Replies : 1
Hi All,
I am using MVC3 in my application. In my controller i have to return Json data to bind my JQGrid.

Based on a condition, i want to build my Json data. For this i need to declare the variable
"var jsonData" (refer below code) and initialize it to some value initially, so that i can build the query and return it at last to bind the JQGrid.

I tried to initialize it in many ways like by giving empty string or assigning it to null etc. But i failed,please tell me How can i initialize it to null?

public JsonResult LoadByName(string txtSearchVal, string SearchBy)
{

//var jsonData= new list<string>;
if (SearchBy.ToUpper() == "FIRSTNAME")
{
var jsonData = new
{
records = tskgrp.UserDetails.Count(),
rows = (
from user in tskgrp.UserDetails
where (user.FirstName.StartsWith(txtSearchVal))
select new
{
i = user.ID,
cell = new string[] { user.ID.ToString(), user.FirstName, user.LastName, user.Active.ToString() }
}).ToArray()
};
}
if (SearchBy.ToUpper() == "LASTNAME")
{
var jsonData = new
{
records = tskgrp.UserDetails.Count(),
rows = (
from user in tskgrp.UserDetails
where (user.FirstName.Contains(txtSearchVal))
select new
{
i = user.ID,
cell = new string[] { user.ID.ToString(), user.FirstName, user.LastName, user.Active.ToString() }
}).ToArray()
};
}
return Json(jsonData, JsonRequestBehavior.AllowGet);
}

Thanks in advance
Sheetal




Responses

Posted by: Umeshdwivedi on: 7/20/2011 [Member] Starter | Points: 25

Up
0
Down
hallo
The simplest solution would be to have 2 returns statements.
If you prefer a cleaner approch, then create a private function for each case.
public JsonResult LoadByName(string txtSearchVal, string SearchBy) 

{
JsonResult result = null;

if (SearchBy.ToUpper() == "FIRSTNAME")
{
result = LoadByFirstName(txtSearchVal);
}
else if (SearchBy.ToUpper() == "LASTNAME")
{
result = LoadByLastName(txtSearchVal);
}
return result;
}

private JsonResult LoadByFirstName(string txtSearchVal)
{
var jsonData = new
{
records = tskgrp.UserDetails.Count(),
rows = (
from user in tskgrp.UserDetails
where (user.FirstName.StartsWith(txtSearchVal))
select new
{
i = user.ID,
cell = new string[]
{
user.ID.ToString(),
user.FirstName,
user.LastName,
user.Active.ToString()
}
}).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}

private JsonResult LoadByLastName(string txtSearchVal)
{
var jsonData = new
{
records = tskgrp.UserDetails.Count(),
rows = (
from user in tskgrp.UserDetails
where (user.LastName.Contains(txtSearchVal))
select new
{
i = user.ID,
cell = new string[]
{
user.ID.ToString(),
user.FirstName,
user.LastName,
user.Active.ToString()
}
}).ToArray()
};

return Json(jsonData, JsonRequestBehavior.AllowGet);
}

By the way, by declaring JsonResult result = null; as I have done, it is not necessary to split the function in 2 functions
Also since your query only varies by the where clause, it is possible to simply use an OR operation to control the filtering.

bool searchFirstName = false;
bool searchLastName = false;

switch (SearchBy.ToUpper())
{
case "FIRSTNAME" :
searchFirstName = true;
break;

case "LASTNAME" :
searchLastName = true;
break;

case null:
case "":
// Unfiltered as is... Adjust if desired.
break;

default:
// Could raise an exception here or return an
// empty result set.
break;
}

//...

var query =
from user in tskgrp.UserDetails
where !searchFirstName || user.FirstName.Contains(txtSearchVal)
where !searchLastName || user.LastName.Contains(txtSearchVal)
select new
{
// ...
};

//...


Although it might be somewhat slower, it will greatly reduce code duplication when a trick like this is used for filtering.



Latest Technology Trainer
And Part time software consultant

Sheetal.Swamy, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response