Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Simple:</p> <pre><code>public ActionResult GetTreeview(string id, string company) { // company is not being used... var parentId = Convert.ToInt32(id); var jsTree = (from category in db.Categories where category.ICG_PARENT_ID == parentId orderby category.ICG_CATEGORY_NAME select new JsTreeModel { data = category.ICG_CATEGORY_NAME, attr = new JsTreeAttribute { id = category.ICG_CATEGORY_ID.ToString() }, state = "closed", }).ToArray(); return Json(jsTree, JsonRequestBehavior.AllowGet); } </code></pre> <p><hr> For the conditional where clause, there's a couple of ways you could do this and they're not going to make much difference in this isolated example. You could either conditionally use one of two queries that has your where clause:</p> <pre><code>JsTreeModel[] jsTree; if (parentId == 0) { var companyId = Convert.ToInt32(company); jsTree = (from category in db.Categories where category.ICG_PARENT_ID == parentId &amp;&amp; category.ICG_COMPANY_ID == companyId //etc... ).ToList(); } else { jsTree = (from category in db.Categories where category.ICG_PARENT_ID == parentId //etc.. ).ToList(); } </code></pre> <p>Or you could break the query where you would have inserted the where clause, conditionally append the additional clause, and finish the query:</p> <pre><code>// get the first part of the query IQueryable&lt;Category&gt; query = db.Categories .Where(category =&gt; category.ICG_PARENT_ID == parentId); // conditionally add the where clause if (parentId == 0) { var companyId = Convert.ToInt32(company); query = query.Where(category =&gt; category.ICG_COMPANY_ID == companyId); } // finish the query var jsTree = query .OrderBy(category =&gt; category.ICG_CATEGORY_NAME) .AsEnumerable() // use LINQ to Objects from this point on .Select(category =&gt; new JsTreeModel { data = category.ICG_CATEGORY_NAME, attr = new JsTreeAttribute { id = category.ICG_CATEGORY_ID.ToString() }, state = "closed", }).ToArray(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload