I have a form on the client side with two drop-down menus, he chooses a year and a type of cover there.
In the controller, I collect this data. And from this I make a query into the table. It is logical that if nothing is selected, then I load all the data.
string sql_count = string.Format("SELECT count(*) FROM labirint"); string sql_pages = string.Format(" ORDER BY id ASC LIMIT "+ skip +", 10"); string sql_all = string.Format("SELECT * FROM labirint"); string sql_result = string.Format(sql_all + sql_pages); var v = dc.Database.SqlQuery<labirint>(sql_result).AsQueryable(); recordsTotal = dc.Database.SqlQuery<int>(sql_count).First(); var data = v.ToList(); dc.Dispose(); return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data }, JsonRequestBehavior.AllowGet); This is how it looks when the table is loaded by default. Now when they choose a year. I'm doing a string splice.
if (!string.IsNullOrEmpty(year)) { int year_int = int.Parse(year); string sql_year = string.Format(" WHERE year = " + year_int + ""); sql_result = string.Format(sql_all + sql_year + sql_pages); sql_count = string.Format(sql_count + sql_year); } The result is like this
string sql_count = string.Format("SELECT count(*) FROM labirint"); string sql_pages = string.Format(" ORDER BY id ASC LIMIT "+ skip +", 10"); string sql_all = string.Format("SELECT * FROM labirint"); string sql_result = string.Format(sql_all + sql_pages); if (!string.IsNullOrEmpty(year)) { int year_int = int.Parse(year); string sql_year = string.Format(" WHERE year = " + year_int + ""); sql_result = string.Format(sql_all + sql_year + sql_pages); sql_count = string.Format(sql_count + sql_year); } var v = dc.Database.SqlQuery<labirint>(sql_result).AsQueryable(); recordsTotal = dc.Database.SqlQuery<int>(sql_count).First(); var data = v.ToList(); dc.Dispose(); return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data }, JsonRequestBehavior.AllowGet); Everything works great. But most likely I wrote it all crooked and not beautiful. The problems start when I want to sort by cover. I chose for example the year 2013 and I want to select all the books with a hard cover or vice versa. How can I do it? Perhaps there will be a third field with dropbox.

dcisDbContext? - koks_rs