使用LINQ将节点列表及其属性列表组合在一起

时间:2014-12-17 14:25:18

标签: c# linq

我有一个包含节点列表(带有属性ID和名称)的表,以及一个带有节点属性列表的第二个表(带有对应节点ID的父对象)。我通常查询节点,然后获取其属性,以填充对象

public class NodeWithAttributes
 {
     public Node Node = new Node();
     public List<NodeAttribute> NodeAttributes = new List<NodeAttribute>();
 }

但是,当我想查询“NodeWithAttributes&#39;事情变得复杂。我试图通过Parent将属性组合在一起,然后将它们分配给对象 - 类似于:

var results = (from n in dbNodes.Nodes
                join na in dbNodes.NodeAttributes on n.ID equals na.Parent
                group na by na.Parent
                into g
                select new NodeWithAttributes
                {
                    Node = n,
                    NodeAttributes = g
                }                                                              
                ).ToList();

但我似乎只能返回该组,而不是基于它的对象。如何返回NodeWithAttributes列表?

1 个答案:

答案 0 :(得分:0)

我认为在您的情况下无需按数据分组。如果您希望Parent Children尝试使用类似于以下的查询。

   var query = from c in north.Customers   
               orderby c.CustomerID
               select new
               {
                  customer = c,
                  orders = c.Orders
               };


        foreach (var item in query)
        {
            Console.WriteLine(item.customer.CompanyName);
            foreach (var ord in item.orders)
            {
                Console.WriteLine(ord.OrderDate + " " + ord.OrderID);
            }
            Console.WriteLine("");
        } 
相关问题