none
请问如何使用Linq实现从两个表提取数据形成一个含列表类的数据集? RRS feed

  • 问题

  • 这个类的类型的定义如下
        public class MyTreeCategory
        { 
            public string CategoryName { get; set; }
            public List<MyTreeProduct> Children { get; set; } 
        }

       public class MyTreeProduct
        {
             public string product_name { get; set; }
             public string navigate_url { get; set; } 
        }


    表的定义如下
    CREATE TABLE [dbo].[business_tree_Category](
    [Category_id] [varchar](20) NOT NULL,
    [Category_name] [varchar](50) NOT NUL
    )
    CREATE TABLE [dbo].[business_tree_Product](
    [category_id] [varchar](20) NOT NULL,
    [product_id] [varchar](20) NOT NULL,
    [product_name] [varchar](50) NOT NULL,
    [navigate_url] [varchar](100) NOT NULL
    )


    就是需要从business_tree_Category,business_tree_Product两个表读取数据,填充类为
    MyTreeCategory的列表

    不知道有没有简单的Linq读取方法 ?

    2014年6月17日 14:40

答案

  • 你好楼主

    从你的表结构中看到你没有定义外键约束,这两个tables 是独立的。我假设楼主用的LINQ2SQL 那么楼主你的linq可以这么些,需要用投影:

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                DataClasses1DataContext db = new DataClasses1DataContext();
    
    
                var result = (from order in db.Orders
    
                              select new OrderDTO
    
                              {
    
                                  OrderName = order.OrderName,
    
                                  OrderDetails = (from od in db.OrderDetails
    
                                                 where od.OrderID == order.OrderID
    
                                                 select new OrderDetailDTO { OrderDetailName = od.OrderDetailName }).ToList()
    
                              }).ToList();
    
            }
    
        }
    
    
        public class OrderDTO
    
        {
    
            public string OrderName { get; set; }
    
    
            public ICollection<OrderDetailDTO> OrderDetails { get; set; }
    
        }
    
    
        public class OrderDetailDTO
    
        {
    
            public string OrderDetailName { get; set; }
    
        }
    

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月18日 3:16
    版主

全部回复

  • 你好楼主

    从你的表结构中看到你没有定义外键约束,这两个tables 是独立的。我假设楼主用的LINQ2SQL 那么楼主你的linq可以这么些,需要用投影:

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                DataClasses1DataContext db = new DataClasses1DataContext();
    
    
                var result = (from order in db.Orders
    
                              select new OrderDTO
    
                              {
    
                                  OrderName = order.OrderName,
    
                                  OrderDetails = (from od in db.OrderDetails
    
                                                 where od.OrderID == order.OrderID
    
                                                 select new OrderDetailDTO { OrderDetailName = od.OrderDetailName }).ToList()
    
                              }).ToList();
    
            }
    
        }
    
    
        public class OrderDTO
    
        {
    
            public string OrderName { get; set; }
    
    
            public ICollection<OrderDetailDTO> OrderDetails { get; set; }
    
        }
    
    
        public class OrderDetailDTO
    
        {
    
            public string OrderDetailName { get; set; }
    
        }
    

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月18日 3:16
    版主
  • 非常感谢您的回复,您的答案很好

    其实写法也挺容易的,只是我对Linq不熟悉

    外键是忘了建了

    2014年6月19日 8:44