5

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

var CurrTable;

该表如下所示:

SaleID | ProductID | ProductName

  1    |     1     |    Book
  1    |     2     |  Notebook
  2    |     3     |   Guitar
  2    |     4     |   Piano

我有两个类,ProductInfo 和 SaleInfo:

public class ProductInfo
{
    int ProductID;
    string ProductName;
}

public class SaleInfo
{
    int SaleID;
    List<ProductInfo> products;
}

我只想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 = (*...*)
                                      }).ToList();

但是不知道在里面写什么(*...*)。我不想再做一次选择,因为我已经有了数据。我只想在产品列表中填写列表,这就是我使用 group 功能的原因。

我怎样才能做到这一点?

4

2 回答 2

5

我怀疑你希望你的 select 子句是:

select new SaleInfo
{
    SaleID = g.Key.SaleId,
    Products = g.Select(x => new ProductInfo { ProductID = x.ProductID,
                                               ProductName = x.ProductName })
                .ToList()
}
于 2012-04-13T09:22:55.060 回答
0

也许是这样的:

select new SaleInfo()
        {
            SaleID=g.Key.SaleId,
            products=g.Select (x =>new ProductInfo(){
              ProductID=x.ProductID,ProductName=x.ProductName}).ToList()
        }

如果您应该能够访问您的属性。我会将课程更改为:

public class ProductInfo
{
    public int ProductID;
    public string ProductName;
}

public class SaleInfo
{
    public int SaleID;
    public List<ProductInfo> products;
}
于 2012-04-13T09:29:34.220 回答