LINQ Query using if condition and && Operation - asp.net-mvc

Controller Method for Search
[HttpPost]
public ActionResult Search(string cno, string fname, string lname, string male, string female, string istateid, string icityid, string professionid, string educationid)
{
var db = new clubDataContext();
var query = db.M_Registarions.Where((x=> x.M_ContactNo ==(cno??x.M_ContactNo) && x.M_Fname == (fname ?? x.M_Fname) && x.M_Lname==(lname ?? x.M_Lname) && x.M_Gender == (male??x.M_Gender));
if(istateid != "")
{
int stateid1 = Convert.ToInt32(istateid);
query = query.Where(x=> x.M_StatteId == stateid1);
}
if(icityid != "")
{
int cityid1 = Convert.ToInt32(icityid);
query = query.Where(x=> x.M_CityId == cityid1);
}
if(professionid != "")
{
int professionid1 = Convert.ToInt32(professionid);
query = query.Where(x=> x.P_id == professionid1);
}
if(educationid != "")
{
int educationid1 = Convert.ToInt32(educationid);
query = query.Where(x=> x.P_id == educationid1);
}
if (!query.Any())
{
ViewBag.Count = 0;
}
else
{
var result = query.ToList();
//var search_query = from p in db.M_Registarions where (x => x.M_Fname.Contains(fname ?? x.M_Fname) || x.M_Lname.Contains(lname ?? x.M_Lname)) select p;
ViewBag.SearchResult = result;
ViewBag.Count = 1;
}
return PartialView("SearchResult");
}
Here I am using OR operation in cno, firstname and last name. I want to use &&(And) operation instead of ||(OR). But problem is I have to check for NULL or Space, that I have done in stateid, cityid , professionid and educationid. So how to use if condition in first where? var query = db.M_Registration.Where(if{}) Like this?

Use blocs {} and "return" to put a more complex expression
For example :
db.M_Registration.Where(p =>
{
if{ // my condition... }
return p.Name == name
else
return p.Name == ...
});

You could try this.
[HttpPost]
public ActionResult Search(string cno, string fname, string lname, string male, string female, string istateid, string icityid, string professionid, string educationid)
{
var db = new clubDataContext();
var query = db.M_Registarions.Where((x=> x.M_ContactNo ==(cno??x.M_ContactNo) && x.M_Fname == (fname ?? x.M_Fname) && x.M_Lname==(lname ?? x.M_Lname) && x.M_Gender == (male??x.M_Gender));
if(IntValue(istateid).HasValue)
{
query = query.Where(x=> x.M_StatteId == IntValue(istateid).Value);
}
if(IntValue(icityid).HasValue)
{
query = query.Where(x=> x.M_CityId == IntValue(icityid).Value);
}
if(IntValue(professionid).HasValue)
{
query = query.Where(x=> x.P_id == IntValue(professionid).Value);
}
if(IntValue(educationid).HasValue)
{
query = query.Where(x=> x.P_id == IntValue(educationid).Value);
}
if (!query.Any())
{
ViewBag.Count = 0;
}
else
{
var result = query.ToList();
//var search_query = from p in db.M_Registarions where (x => x.M_Fname.Contains(fname ?? x.M_Fname) || x.M_Lname.Contains(lname ?? x.M_Lname)) select p;
ViewBag.SearchResult = result;
ViewBag.Count = 1;
}
return PartialView("SearchResult");
}
public int? IntValue(string input)
{
int temp;
if (int.TryParse(input, out temp))
{
return temp;
}
return null;
}

I think you could get the result you want and avoid having tons of ifs, by trying the following query:
var query =
db.M_Registarions.Where(
x =>
x.M_ContactNo == (cno ?? x.M_ContactNo) && x.M_Fname == (fname ?? x.M_Fname)
&& x.M_Lname == (lname ?? x.M_Lname) && x.M_Gender == (male ?? x.M_Gender)
&& (string.IsNullOrEmpty(istateid) || x.M_StatteId == Convert.ToInt32(istateid))
&& (string.IsNullOrEmpty(icityid) || x.M_CityId == Convert.ToInt32(icityid))
&& (string.IsNullOrEmpty(professionid) || x.P_id == Convert.ToInt32(professionid))
&& (string.IsNullOrEmpty(educationid) || x.P_id == Convert.ToInt32(educationid)));

Related

Getting same Current and Original value of change tracker modified state

Below is my override saveChanges Methed which calls SetChanges Method
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
SetChanges();
OnBeforeSaving();
return base.SaveChanges(acceptAllChangesOnSuccess);
}
Right now, Sometimes code works completely fine but in some scenario It gives same value of both property.OriginalValue and property.CurrentValue for Modification so I am not able find what is the issue in my code
private void SetChanges()
{
Guid SystemLogId = Guid.NewGuid();
var currentDate = DateTime.Now;
var entitiesTracker = ChangeTracker.Entries()
.Where(p => p.State == EntityState.Modified || p.State == EntityState.Added).ToList();
foreach (var entry in entitiesTracker)
{
var pagename = entry.Entity.GetType().Name;
if (pagename != "ExceptionLog")
{
var rowid = 0;
try
{
rowid = int.Parse(entry.OriginalValues["Id"].ToString());
}
catch (Exception)
{ }
SystemLog sysLog = new SystemLog();
List<SystemChangeLog> changeLog = new List<SystemChangeLog>();
foreach (PropertyEntry property in entry.Properties)
{
string propertyName = property.Metadata.Name;
switch (entry.State)
{
case EntityState.Added:
sysLog.Event = "Created";
break;
case EntityState.Modified:
{
sysLog.Event = "Updated";
if (propertyName != "ModifiedDate" && propertyName != "CreatedDate" && propertyName != "ModifiedBy" && propertyName != "CreatedBy" && propertyName != "RowVersion")
{
var original = Convert.ToString(property.OriginalValue);
var current = Convert.ToString(property.CurrentValue);
if (property.IsModified && !original.Equals(current))
{
SystemChangeLog log = new SystemChangeLog()
{
Property = propertyName,
OldValue = original,
NewValue = current,
DateOfChange = currentDate,
rowid = rowid,
SystemLogId = SystemLogId.ToString(),
};
changeLog.Add(log);
}
}
}
break;
}
}
base.Set<SystemChangeLog>().AddRange(changeLog);
if(changeLog.Count() >0 || entry.State == EntityState.Added)
{
sysLog.UserId = UserId;
sysLog.Date = currentDate;
sysLog.Page = pagename;
sysLog.Location = ExceptionHandler(entry, "Location");
sysLog.IPAddress = ExceptionHandler(entry, "IPAddress");
sysLog.MACAddress = ExceptionHandler(entry, "MACAddress");
sysLog.SystemLogId = SystemLogId.ToString();
base.Set<SystemLog>().Add(sysLog);
}
}
}
}
And also Is there any way to make it fast for more than thousand entry
hope below code can help:
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
setChanges(); // to get new value and old value
var result = base.SaveChanges(acceptAllChangesOnSuccess);
OnAfterSaveChanges();// to get auto added id
return result;
}

OpenXML not replacing FieldCode which having more than 41characters as display text not with result values

While a Field Code has more than 40 characters then OpenXML does not replace FieldCode with result values also Formating is not getting applied for the same. The below is working fine filed codes which have up to 40 characters but failed for more than 40 characters.
var format = "";
foreach (var paragraph in wordDocument.MainDocumentPart.RootElement.Descendants())
{
foreach (FieldCode fieldCode in paragraph.Descendants())
{
if (!String.IsNullOrEmpty(fieldCode.InnerText) && fieldCode.InnerText.Trim() != null && fieldCode.InnerText.Trim() != ""
&& (fieldCode.InnerText.Contains('#') || fieldCode.InnerText.Contains('#')))
{
if (fieldCode.InnerText.Contains('#'))
{
var indexofsymbol = fieldCode.InnerText.IndexOf('#') + 1;
format = fieldCode.InnerText.Substring(indexofsymbol, fieldCode.InnerText.Length - indexofsymbol);
}
else if (fieldCode.InnerText.Contains('#'))
{
var indexofsymbol = fieldCode.InnerText.IndexOf('#') + 1;
format = fieldCode.InnerText.Substring(indexofsymbol, fieldCode.InnerText.Length - indexofsymbol);
}
Run xxxfield = (Run)fieldCode.Parent;
Run rBegin = xxxfield.PreviousSibling<Run>();
Run rSep = xxxfield.NextSibling<Run>();
Run rText = rSep.NextSibling<Run>();
Run rEnd = rText.NextSibling<Run>();
if (null != xxxfield)
{
Text t = rEnd.GetFirstChild<Text>();
t = xxxfield.Descendants<Text>().FirstOrDefault();
if (t == null)
t = rText.GetFirstChild<Text>();
if (t != null)
{
var fieldName = t.Text.ToString().Substring(1, t.Text.Length - 2);
if (mergeFields.TryGetValue(fieldName, out dictValue))
{
if (fieldCode.InnerText.Contains('#'))
{
DateTime date = DateTime.Parse(dictValue);
t.Text = date.ToString(format); //Convert.ToDateTime(dictValue).ToString(format);
}
else if (fieldCode.InnerText.Contains('#'))
{
t.Text = Convert.ToDouble(dictValue).ToString(format);
}
}
}
}
}
}
}

how to save list of data in memory?

I have an ajax function which is called by the jquery-datatable and ve two responsibility.
To get data from the database.
To serve the search, sort, pagination like functional work.
Now all I need is I just wanna get data once and save it in memory so that when user type something in the search box it performs the search from stored data directly.
Here the code.
public ActionResult AjaxOil(JQueryDataTableParamModel param)
{
//To get data and should be run only once.
IEnumerable<Oil> allOils = _context.Oils.ToList();
//All others function.
IEnumerable<Oil> filteredOils;
if (!string.IsNullOrEmpty(param.sSearch))
{
filteredOils = allOils
.Where(c => c.CommonName.Contains(param.sSearch)
||
c.BotanicalName.Contains(param.sSearch)
||
c.PlantParts.Contains(param.sSearch)
||
c.Distillation.Contains(param.sSearch));
}
else
{
filteredOils = allOils;
}
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
Func<Oil, string> orderingFunction = (c => sortColumnIndex == 1 ? c.CommonName :
sortColumnIndex == 2 ? c.BotanicalName :
c.PlantParts);
var distillationFilter = Convert.ToString(Request["sSearch_4"]);
var commonFilter = Convert.ToString(Request["sSearch_1"]);
var botanicalFilter = Convert.ToString(Request["sSearch_2"]);
var plantFilter = Convert.ToString(Request["sSearch_3"]);
if (!string.IsNullOrEmpty(commonFilter))
{
filteredOils = filteredOils.Where(c => c.CommonName.Contains(commonFilter));
}
if (!string.IsNullOrEmpty(botanicalFilter))
{
filteredOils = filteredOils.Where(c => c.BotanicalName.Contains(botanicalFilter));
}
if (!string.IsNullOrEmpty(plantFilter))
{
filteredOils = filteredOils.Where(c => c.PlantParts.Contains(plantFilter));
}
if (!string.IsNullOrEmpty(distillationFilter))
{
filteredOils = filteredOils.Where(c => c.Distillation.Contains(distillationFilter));
}
var sortDirection = Request["sSortDir_0"];
if (sortDirection == "asc")
filteredOils = filteredOils.OrderBy(orderingFunction);
else
filteredOils = filteredOils.OrderByDescending(orderingFunction);
var displayedOils = filteredOils
.Skip(param.iDisplayStart)
.Take(param.iDisplayLength);
var result = from c in displayedOils
select new[] { Convert.ToString(c.OilId), c.CommonName, c.BotanicalName, c.PlantParts, c.Distillation };
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = allOils.Count(),
iTotalDisplayRecords = filteredOils.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet);
On first load save the data in cache/session/static field. On next search check if the cache/session/static field is not null and read from there, not from db, else take again from db..
Example:
private static ObjectCache _cache = new MemoryCache("MemoryCache");
public List<Oils> GetDataFromCache(string keyName)
{
//private static ObjectCache _cache = new MemoryCache("keyName");
var data = _cache.Get(keyName);
if (data != null) return data as List<Oils>;
data = _context.Oils.ToList();
//keep the cache for 2h
_cache.Add(keyName, data, DateTimeOffset.Now.AddHours(2));
return data;
}
(didn't test the code, but that's the logic) or you can use Session if you prefer
Session example:
if(Session["Data_Oils"] != null) { return Session["Data_Oils"] as List<Oils; } else { var temp = _context.Oils.ToList(); Session["Data_Oils"] = temp; return temp; }

Struts2 java.sql.Timestamp to Calendar type conversion

In an Struts 2 Action class, there is List<Object[]> object. One of the elements of each Object array is java.sql.Timestamp, which I display using <s:date> Struts tag. Can I convert data from Timestamp to UTC calendar using application level conversion? I tried using a custom converter and added following line into xwork-conversion.properties:
java.sql.Timestamp=com.test.CalendarConverter
But, this did not affect the output and the convertToString method of CalendarConverter was not even called. Below is the CalendarConverter
public class CalendarConverter extends StrutsTypeConverter {
Log logger = LogFactory.getLog(CalendarConverter.class);
String format = null;
public CalendarConverter() {
format = "MM/dd/yyyy HH:mm";
}
public Object convertFromString(Map context, String [] values, Class toClass) {
Calendar cal = null;
logger.debug("-----------------> In convertFromString: value[0]- " + ((values == null || values.length < 1) ? "NULL" : values[0]));
if (values != null && values.length == 1) {
if (values[0].trim().length() > 0) {
try {
TimeZone tz = TimeZone.getDefault();
tz = userSession.getTimeZone();
if (tz == null) {
tz = TimeZone.getDefault();
}
DateFormat dateFormat = null;
if (isValidDate(values[0], format)) {
dateFormat = new SimpleDateFormat(format);
}
dateFormat.setLenient(false);
dateFormat.setTimeZone(tz);
Date dt = dateFormat.parse(values[0]);
cal = Calendar.getInstance(tz);
cal.setTime(dt);
} catch (Exception ex) {
logger.error("Error in date conversion");
logger.debug(ex.getMessage());
//throw new TypeConversionException(ex);
}
}
}
return cal;
}
public boolean isValidDate(String str, String format) {
DateFormat df = new SimpleDateFormat(format);
df.setLenient(false);
ParsePosition pos = new ParsePosition(0);
df.parse(str, pos);
if (pos.getErrorIndex() == -1 && pos.getIndex() == str.length() )
return true;
else
return false;
}
public String convertToString(Map context, Object o) {
String data = null;
DateFormat dateFormat = new SimpleDateFormat(format);
TimeZone tz = TimeZone.getDefault();
tz = TimeZone.getTimeZone("UTC");
if (tz == null) {
tz = TimeZone.getDefault();
}
dateFormat.setTimeZone(tz);
logger.debug("-----------------> In convertToString: o- " + o + " (" + (o == null ? null : o.getClass().getName()) + ")");
if (o != null) {
if (o instanceof Calendar) {
Calendar cal = (Calendar)o;
data = dateFormat.format(cal.getTime());
} else if (o instanceof Date) {
data = dateFormat.format((Date)o);
} else if (o instanceof Timestamp) {
data = dateFormat.format((Timestamp)o);
}
}
return data;
}
}

What will be the time complexity of reversing the linked list in a different way using below code?

Given a linked List $link1, with elements (a->b->c->d->e->f->g->h->i->j), we need to reverse the linked list provided that the reversing will be done in a manner like -
Reverse 1st element (a)
Reverse next 2 elements (a->c->b)
Reverse next 3 elements (a->c->b->f->e->d)
Reverse next 4 elements (a->c->b->f->e->d->j->i->h->g)
....
....
I have created below code in PHP to solve this problem
Things I need -
I need to calculate the time complexity of reverseLinkedList function below.
Need to know if we can optimize reverseLinkedList function to reduce time complexity.
-
class ListNode
{
public $data;
public $next;
function __construct($data)
{
$this->data = $data;
$this->next = NULL;
}
function read_node()
{
return $this->data;
}
}
class LinkList
{
private $first_node;
private $last_node;
private $count;
function __construct()
{
$this->first_node = NULL;
$this->last_node = NULL;
$this->count = 0;
}
function size()
{
return $this->count;
}
public function read_list()
{
$listData = array();
$current = $this->first_node;
while($current != NULL)
{
echo $current->read_node().' ';
$current = $current->next;
}
}
public function reverse_list()
{
if(($this->first_node != NULL)&&($this->first_node->next != NULL))
{
$current = $this->first_node;
$new = NULL;
while ($current != NULL)
{
$temp = $current->next;
$current->next = $new;
$new = $current;
$current = $temp;
}
$this->first_node = $new;
}
}
public function read_node($position)
{
if($position <= $this->count)
{
$current = $this->first_node;
$pos = 1;
while($pos != $position)
{
if($current->next == NULL)
return null;
else
$current = $current->next;
$pos++;
}
return $current->data;
}
else
return NULL;
}
public function insert($data)
{
$new_node = new ListNode($data);
if($this->first_node != NULL)
{
$this->last_node->next = $new_node;
$new_node->next = NULL;
$this->last_node = &$new_node;
$this->count++;
}
else
{
$new_node->next = $this->first_node;
$this->first_node = &$new_node;
if($this->last_node == NULL)
$this->last_node = &$new_node;
$this->count++;
}
}
}
//Create linked list
$link1 = new LinkList();
//Insert elements
$link1->insert('a');
$link1->insert('b');
$link1->insert('c');
$link1->insert('d');
$link1->insert('e');
$link1->insert('f');
$link1->insert('g');
$link1->insert('h');
$link1->insert('i');
$link1->insert('j');
echo "<b>Input :</b><br>";
$link1->read_list();
//function to reverse linked list in specified manner
function reverseLinkedList(&$link1)
{
$size= $link1->size();
if($size>2)
{
$link2=new LinkList();
$link2->insert($link1->read_node(1));
$elements_covered=1;
//reverse
$rev_size=2;
while($elements_covered<$size)
{
$start=$elements_covered+1;
$temp_link = new LinkList();
$temp_link->insert($link1->read_node($start));
for($i=1;$i<$rev_size;$i++)
{
$temp_link->insert($link1->read_node(++$start));
}
$temp_link->reverse_list();
$temp_size=$temp_link->size();
$link2_size=$link2->size();
for($i=1;$i<=$temp_size;$i++)
{
$link2->insert($temp_link->read_node($i));
++$elements_covered;
++$link2_size;
}
++$rev_size;
}
///reverse
//Flip the linkedlist
$link1=$link2;
}
}
///function to reverse linked list in specified manner
//Reverse current linked list $link1
reverseLinkedList($link1);
echo "<br><br><b>Output :</b><br>";
$link1->read_list();
It's O(n)...just one traversal.
And secondly, here tagging it in language is not necessary.
I have provided a Pseudocode here for your reference:
current => head_ref
prev => NULL;
current => head_ref;
next => null;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;

Resources