首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么将数据LINQ到LIST<object>

2012-08-02 
如何将数据LINQ到LISTobject求一 关于 ListCustomerTree 的Linq表达式1.sql语句selectfcustno, forder

如何将数据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(); 

热点排行