日期:2014-05-18  浏览次数:20884 次

如何将数据LINQ到LIST<object>

求一 关于 List<CustomerTree> 的 Linq表达式


1. sql 语句
 select fcustno, forderno, forderdate from t_orders
 
2. db 数据
fcustno forderno forderdate
1010 101002 2003/4/25
1010 101001 2003/4/25
1010 101002 2003/4/25
1020 102001 2003/4/25
1020 102002 2003/4/25
 
3.model 对象
  public class CustomerTree
  {
  public string customerId { get; set; }  
  public List<orderInfo> orders { get; set; }  
  }
 
  public class orderInfo
  {
  public string orderNo { get; set; }
  public DateTime orderDate { get; set; }  
  }
 
4.期望 结果

List<CustomerTree> trees 按照第1列分组 讲第2列第3列 放到orderInfo集合中




------解决方案--------------------
C# code
List<CustomerTree> query = db.GroupBy(x => x.fcustno)
              .Select(x => new CustomerTree()
                               {
                                   customerId = x.Key,
                                   orders = db.Where(y => y.fcustno == x.Key)
                                              .Select(y => new orderInfo()
                                                               {
                                                                   orderNo = y.forderno,
                                                                   orderDate = forderdate
                                                               }).ToList()
                               }).ToList();