我如何在具有parentID的表中写入查询记录,条件为parentID == 0且ID != (parentID)

发布于 2024-10-28 10:08:45 字数 929 浏览 1 评论 0原文

这里我的 LINQ 查询获取表菜单中的记录,条件是parentID == 0(获取根菜单)和ID!=(parentID列表)(这是父ID列表是有子菜单记录的id),我只是想要加载所有记录,包括没有子记录的根菜单和子记录:

List<Menu> menus = MenuDAO.Instance.GetAll(); // Get All Record in Menu Table
var parentID = (from p in menus where p.ParentID != 0 select new {p.ParentID}).Distinct(); // Get unique ParentID in Menu Table
        List<int> numParentID = new List<int>();
        foreach (var a in parentID)
        {
            numParentID.Add(a.ParentID);
        } // assign to a list <int>
        this.ddlMenu.DataSource = from m1 in menus
                                  where !(numParentID).Contains((int)m1.ID) && m1.ParentID == 0
                                  select new { m1.ID, m1.Name };
        this.ddlMenu.Databind();

我运行此代码,我显示没有子记录的记录,不显示子记录。有人帮我解决它。我刚接触 LINQ,非常感谢。

我期望的结果是:没有任何子项的记录列表,我的菜单表架构是:ID、名称、订单、ParentID。

Here i my LINQ query to get record in Table Menu with condition are parentID == 0(get root menu) and ID != (parentID list) (which is parent ID list is are id of menu record that have child), i just want to load all record includes root menu that have no children record and children record :

List<Menu> menus = MenuDAO.Instance.GetAll(); // Get All Record in Menu Table
var parentID = (from p in menus where p.ParentID != 0 select new {p.ParentID}).Distinct(); // Get unique ParentID in Menu Table
        List<int> numParentID = new List<int>();
        foreach (var a in parentID)
        {
            numParentID.Add(a.ParentID);
        } // assign to a list <int>
        this.ddlMenu.DataSource = from m1 in menus
                                  where !(numParentID).Contains((int)m1.ID) && m1.ParentID == 0
                                  select new { m1.ID, m1.Name };
        this.ddlMenu.Databind();

And i run this code , i display record that have no children, do not display chilren record. Somebody help me fix it. My new in LINQ , thanks a lot.

The Result as i expect here is : list of record that do not have any children, my Menu table schema is : ID, Name, Order, ParentID.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

请你别敷衍 2024-11-04 10:08:46

建议

1-您不需要在第一次选择中选择匿名对象,您可以像

var parentIDs = (from p in menus 
                 where p.ParentID != 0 
                 select p.ParentID).Distinct();

往常一样编写将集合命名为复数(parentIDs)的好习惯

2-无需迭代来创建new List<>,这样您就可以在一个查询中写入所有内容

  List<int> numParentIDs = (from p in menus 
                            where p.ParentID != 0 
                            select p.ParentID).Distinct().ToList();

答案:
首先选择所有叶级子 ID。获取除 ParentID 列中的值之外的所有 ID。然后通过加入 leafID 从菜单中进行选择

var leafMenuIDs = menus
                    .Select(m => m.ID)
                    .Except(menus.Select(m => m.ParentID).Distinct())                                         
                    .Distinct();


 this.ddlMenu.DataSource = from m in menus
                           join id in leafMenuIDs on m.ID equals id
                           select new { m.ID, m.Name };

Suggestions

1-You don't need to select an anonymous object in the first select, you could write as

var parentIDs = (from p in menus 
                 where p.ParentID != 0 
                 select p.ParentID).Distinct();

always a good practice to name collections as plural (parentIDs)

2-No need to iterate to create a new List<>, so you can write all of them in one query

  List<int> numParentIDs = (from p in menus 
                            where p.ParentID != 0 
                            select p.ParentID).Distinct().ToList();

Answer :
first select all the leaf level children IDs. Get all ID except the values in the ParentID column. And then do a select from menu by joining the leafIDs

var leafMenuIDs = menus
                    .Select(m => m.ID)
                    .Except(menus.Select(m => m.ParentID).Distinct())                                         
                    .Distinct();


 this.ddlMenu.DataSource = from m in menus
                           join id in leafMenuIDs on m.ID equals id
                           select new { m.ID, m.Name };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文