Linq 到自定义数组对象
我正在使用 MVC3/Entity Framework,我需要发送一个 JSON 对象以供 jsTree 使用。每次展开节点时,jsTree 都会调用子节点,因此每次 get 仅返回一组,而不是整个树。这是 get 函数:
public ActionResult GetTreeview(string id, string company)
{
int parentId = Convert.ToInt32(id);
var cats = (from x in db.Categories where x.ICG_PARENT_ID == parentId orderby x.ICG_CATEGORY_NAME select new { x.ICG_CATEGORY_ID, x.ICG_PARENT_ID, x.ICG_CATEGORY_NAME }).ToList();
var jsTree = new JsTreeModel[cats.Count];
for (int i = 0; i < cats.Count; i++)
{
jsTree[i] = new JsTreeModel
{
data = cats[i].ICG_CATEGORY_NAME,
attr = new JsTreeAttribute { id = cats[i].ICG_CATEGORY_ID },
state = "closed"
};
}
return Json(jsTree, JsonRequestBehavior.AllowGet);
}
这是模型:
public class JsTreeModel
{
public string data;
public string state;
public JsTreeAttribute attr;
}
public class JsTreeAttribute
{
public int id;
public bool selected;
}
我确信一定存在更短/更好的方法来执行此操作,但我最近刚刚开始学习 LINQ 和 lambda 表达式(和 MVC),它们对我来说还不是很直观。另外,我已经使用这个网站作为资源很长时间了(非常棒),但这是我的第一个问题。真的没有预览按钮吗,还是我瞎了?
编辑:更新模型和函数以将 attr.Id 的类型更改为 int。 LINQ to Entities 不喜欢字符串转换。
I am using MVC3/Entity Framework, I need to send a JSON object to be used by a jsTree. The jsTree is calling for the children each time a node is expanded, so each get returns only one set, instead of the whole tree. This is the get function:
public ActionResult GetTreeview(string id, string company)
{
int parentId = Convert.ToInt32(id);
var cats = (from x in db.Categories where x.ICG_PARENT_ID == parentId orderby x.ICG_CATEGORY_NAME select new { x.ICG_CATEGORY_ID, x.ICG_PARENT_ID, x.ICG_CATEGORY_NAME }).ToList();
var jsTree = new JsTreeModel[cats.Count];
for (int i = 0; i < cats.Count; i++)
{
jsTree[i] = new JsTreeModel
{
data = cats[i].ICG_CATEGORY_NAME,
attr = new JsTreeAttribute { id = cats[i].ICG_CATEGORY_ID },
state = "closed"
};
}
return Json(jsTree, JsonRequestBehavior.AllowGet);
}
and here is the model:
public class JsTreeModel
{
public string data;
public string state;
public JsTreeAttribute attr;
}
public class JsTreeAttribute
{
public int id;
public bool selected;
}
I am sure a shorter/better way to do this must exist, but I just recently started learning LINQ and lambda expressions (and MVC) and they are not quite intuitive to me yet. Also, I have been using this site as a resource for a long time (it's wonderful) but this is my first question. Is there really no preview button, or am I blind?
EDIT: Updated Model and Function to change the type of attr.Id to int. LINQ to Entities didn't like string conversion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单的:
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:
或者,您可以在插入 where 子句的位置中断查询,有条件地附加附加子句,然后完成查询:
Simple:
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:
Or you could break the query where you would have inserted the where clause, conditionally append the additional clause, and finish the query:
尝试:
Try: