public void FilterPagingSortingSearch(int?companyID) { if (datatables.iSortCol[0] != (datatables.colums.Count() - 1)) { this.OrderBy = string.Format("ORDER BY {0} {1}", datatables.colums[datatables.iSortCol[0]], datatables.sSortDir[0]); } if (datatables.sSearch != "") { WhereConditions = "WHERE ("; foreach (string column in datatables.colums) { if (column != "ID") { WhereConditions += string.Format("{0} LIKE '%{1}%' OR ", column, datatables.sSearch); } } WhereConditions += "**"; WhereConditions = WhereConditions.Replace("OR **", "") + string.Format(") AND ({0})", datatables.secundaryWhere); } else { if (string.IsNullOrEmpty(WhereConditions)) { WhereConditions += string.Format("WHERE ", companyID); } int index = 0; bool hasFilter = false; foreach (string column in datatables.ffColums) { string filterColumName = column.Split('(')[0]; string patern = column.Split('(')[1].Replace(")", ""); patern = patern.Replace("value", datatables.ffvalues[index]); if (datatables.ffvalues[index] != "") { WhereConditions += string.Format("{0} {1} AND ", filterColumName, patern); hasFilter = true; } index++; } WhereConditions += "**"; WhereConditions = WhereConditions.Replace("AND **", "").Replace("**", "") + string.Format(" {1} ({0})", datatables.secundaryWhere, (hasFilter)?"AND":""); } if (datatables.companyRules && companyID != null) { if (!string.IsNullOrEmpty(WhereConditions)) { WhereConditions += string.Format(" AND (companyID={0})", companyID); } else { WhereConditions += string.Format("WHERE (companyID={0})", companyID); } } fullQuery = string.Format("select {0} from {1} {2} {3}", datatables.rawColums, datatables.tableName, WhereConditions, OrderBy); }