Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用LINQ填充对象列表_Linq_Entity Framework_Linq To Sql_Linq To Entities_Linq To Objects - Fatal编程技术网

用LINQ填充对象列表

用LINQ填充对象列表,linq,entity-framework,linq-to-sql,linq-to-entities,linq-to-objects,Linq,Entity Framework,Linq To Sql,Linq To Entities,Linq To Objects,我创建了一个表,并将其插入 var CurrTable; 该表如下所示: SaleID | ProductID | ProductName 1 | 1 | Book 1 | 2 | Notebook 2 | 3 | Guitar 2 | 4 | Piano List<SaleInfo> results = (from s in CurrTable.AsEnum

我创建了一个表,并将其插入

var CurrTable;
该表如下所示:

SaleID | ProductID | ProductName

  1    |     1     |    Book
  1    |     2     |  Notebook
  2    |     3     |   Guitar
  2    |     4     |   Piano
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();
select new SaleInfo()
        {
            SaleID=g.Key.SaleId,
            products=g.Select (x =>new ProductInfo(){
              ProductID=x.ProductID,ProductName=x.ProductName}).ToList()
        }
我有两个类,ProductInfo和SaleInfo:

public class ProductInfo
{
    int ProductID;
    string ProductName;
}

public class SaleInfo
{
    int SaleID;
    List<ProductInfo> products;
}
公共类ProductInfo
{
int-ProductID;
字符串名称;
}
公共类销售信息
{
国际萨利德;
列出产品清单;
}
我只想用数据填写SaleInfo的列表(
list

我编写的查询如下所示:

SaleID | ProductID | ProductName

  1    |     1     |    Book
  1    |     2     |  Notebook
  2    |     3     |   Guitar
  2    |     4     |   Piano
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();
select new SaleInfo()
        {
            SaleID=g.Key.SaleId,
            products=g.Select (x =>new ProductInfo(){
              ProductID=x.ProductID,ProductName=x.ProductName}).ToList()
        }
List results=(来自CurrTable.AsEnumerable()中的s)
由新的{s.SaleId}将s分组为g
选择新建SaleInfo
{
SaleID=g.Key.SaleID,
产品=(*…*)
}).ToList();
但是我不知道在
(*…*)
中写什么。我不想再做一次选择,因为我已经有了数据。我只想在产品列表中填充列表,这就是我使用group函数的原因


我该怎么做?

我怀疑您希望select子句是:

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

也许是这样的:

SaleID | ProductID | ProductName

  1    |     1     |    Book
  1    |     2     |  Notebook
  2    |     3     |   Guitar
  2    |     4     |   Piano
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();
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;
}
公共类ProductInfo
{
公共int ProductID;
公共字符串产品名称;
}
公共类销售信息
{
公共卫生署;
公开上市产品;
}

你知道如果你对答案很好,你可以考虑接受它吗?如果我不想把它变成一个列表怎么办?如果我想把它放在一个储物箱里怎么办?这有意义吗?有可能吗?@LuisGouveia:嗯,你可以不打电话给
ToList
。。。但是如果你不了解你的背景,很难说这是否能满足你的要求。上帝保佑你!我已经做了整整一周了!现在我可以休息一下了!:D