Linq自定义数组对象

时间:2011-06-16 20:20:05

标签: c# jquery linq jstree

我正在使用MVC3 / Entity Framework,我需要发送一个jsTree使用的JSON对象。每次扩展节点时,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不喜欢字符串转换。

2 个答案:

答案 0 :(得分:2)

简单:

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);
}

<小时/> 对于条件where子句,有几种方法可以做到这一点,它们在这个孤立的例子中不会有太大的区别。您可以有条件地使用具有where子句的两个查询之一:

JsTreeModel[] jsTree;
if (parentId == 0)
{
    var companyId = Convert.ToInt32(company);
    jsTree = (from category in db.Categories
              where category.ICG_PARENT_ID == parentId
                 && category.ICG_COMPANY_ID == companyId
              //etc...
             ).ToList();

}
else
{

    jsTree = (from category in db.Categories
              where category.ICG_PARENT_ID == parentId
              //etc..
             ).ToList();
}

或者您可以在查询中断插入where子句,有条件地附加附加子句并完成查询:

// get the first part of the query
IQueryable<Category> query = db.Categories
    .Where(category => category.ICG_PARENT_ID == parentId);

// conditionally add the where clause
if (parentId == 0)
{
    var companyId = Convert.ToInt32(company);
    query = query.Where(category => category.ICG_COMPANY_ID == companyId);
}

// finish the query
var jsTree = query
    .OrderBy(category => category.ICG_CATEGORY_NAME)
    .AsEnumerable() // use LINQ to Objects from this point on
    .Select(category => new JsTreeModel
     {
         data = category.ICG_CATEGORY_NAME,
         attr = new JsTreeAttribute { id = category.ICG_CATEGORY_ID.ToString() },
         state = "closed",
     }).ToArray();

答案 1 :(得分:0)

尝试:

var jsTree = (from cat in cats
             select new JsTreeModel
             {
                 data = cat.ICG_CATEGORY_NAME, 
                 attr = new JsTreeAttribute { id = cat.ICG_CATEGORY_ID.ToString() }, 
                 state = "closed"
             }).ToArray();