小编Era*_*ran的帖子

使用 LINQ 填充对象列表

我有一个我创建的表,我将它插入到

var CurrTable;
Run Code Online (Sandbox Code Playgroud)

该表如下所示:

SaleID | ProductID | ProductName

  1    |     1     |    Book
  1    |     2     |  Notebook
  2    |     3     |   Guitar
  2    |     4     |   Piano
Run Code Online (Sandbox Code Playgroud)

我有两个类,ProductInfo 和 SaleInfo:

public class ProductInfo
{
    int ProductID;
    string ProductName;
}

public class SaleInfo
{
    int SaleID;
    List<ProductInfo> products;
}
Run Code Online (Sandbox Code Playgroud)

我只想List<SaleInfo>用数据填充 SaleInfo ( )列表...

我写的查询如下所示:

List<SaleInfo> results = (from s in CurrTable.AsEnumerable()
                                      group s by new { s.SaleId} into g
                                      select new SaleInfo
                                      {
                                          SaleID = g.Key.SaleId,
                                          Products …
Run Code Online (Sandbox Code Playgroud)

linq linq-to-objects linq-to-entities entity-framework linq-to-sql

5
推荐指数
1
解决办法
3200
查看次数