Linq 到自定义数组对象

发布于 2024-11-16 02:21:00 字数 1277 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗梦空间 2024-11-23 02:21:00

简单的:

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


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:

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

Simple:

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


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:

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

Or you could break the query where you would have inserted the where clause, conditionally append the additional clause, and finish the query:

// 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();
无敌元气妹 2024-11-23 02:21:00

尝试:

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

Try:

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文