C# 如何从所有产品中获得制造商?

C# 如何从所有产品中获得制造商?,c#,entity-framework,linq,linq-to-entities,C#,Entity Framework,Linq,Linq To Entities,以下 model.Manufacturers = PagedData.Products.Manufacturers.Select(t => new Manufacturer { ManufacturerID = t.ManufacturerID, ManufacturerName = t.ManufacturerName }).AsEnumerable(); 返回 “System.Collections.Generic.IEnumerable”不包含 “制造商”和无扩展方法“制造商”的

以下

model.Manufacturers = PagedData.Products.Manufacturers.Select(t => new Manufacturer {  ManufacturerID = t.ManufacturerID, ManufacturerName = t.ManufacturerName }).AsEnumerable();
返回

“System.Collections.Generic.IEnumerable”不包含 “制造商”和无扩展方法“制造商”的定义 接受类型为的第一个参数 可以找到“System.Collections.Generic.IEnumerable”(是 是否缺少using指令或程序集引用?)

虽然这一个,工程部分如预期,因为它为第一个产品找到了制造商

 var firstOrDefault = PagedData.Products.FirstOrDefault();
 if (firstOrDefault != null) model.Manufacturers = firstOrDefault.Manufacturers.Select(t => new Manufacturer {  ManufacturerID = t.ManufacturerID, ManufacturerName = t.ManufacturerName }).AsEnumerable();

我怎样才能找到所有产品的制造商呢?

您必须先将列表展平。您可以使用
SelectMany
方法执行此操作

model.Manufacturers = PagedData.Products
                               .SelectMany(product=>product.Manufacturers)
                               .Select(m => new Manufacturer 
                                {
                                    ManufacturerID = m.ManufacturerID, 
                                    ManufacturerName = m.ManufacturerName 
                                })
                               .AsEnumerable();

根据您的代码,我假设每个产品都有一个制造商列表。基于此,很明显Products.Manufactures是制造商列表(或制造商序列的序列)。所以这就是为什么你要把你的名单弄平

你必须先把你的名单展平。您可以使用
SelectMany
方法执行此操作

model.Manufacturers = PagedData.Products
                               .SelectMany(product=>product.Manufacturers)
                               .Select(m => new Manufacturer 
                                {
                                    ManufacturerID = m.ManufacturerID, 
                                    ManufacturerName = m.ManufacturerName 
                                })
                               .AsEnumerable();

根据您的代码,我假设每个产品都有一个制造商列表。基于此,很明显Products.Manufactures是制造商列表(或制造商序列的序列)。所以这就是为什么你要把你的名单弄平

之所以出现此错误,是因为
Products
是某个类的集合,但您试图访问某个特定属性,就好像它只是该类的一个实例一样

您可以遍历列表:

foreach(var product in PagedData.Products)
{
    model.Manufacturers.AddRange(
        product.Manufacturers.Select(t => new Manufacturer
                                              {
                                                  ManufacturerID = t.ManufacturerID,
                                                  ManufacturerName = t.ManufacturerName
                                              }));
}
或使用LINQ展平列表:

model.Manufacturers =
    PagedData.Products.SelectMany(p => p.Manufacturers
                                        .Select(t => new Manufacturer
                                                         {
                                                             ManufacturerID = t.ManufacturerID,
                                                             ManufacturerName = t.ManufacturerName})
                                        .AsEnumerable());

出现此错误是因为
Products
是某个类的集合,但您试图访问特定属性,就好像它只是该类的单个实例一样

您可以遍历列表:

foreach(var product in PagedData.Products)
{
    model.Manufacturers.AddRange(
        product.Manufacturers.Select(t => new Manufacturer
                                              {
                                                  ManufacturerID = t.ManufacturerID,
                                                  ManufacturerName = t.ManufacturerName
                                              }));
}
或使用LINQ展平列表:

model.Manufacturers =
    PagedData.Products.SelectMany(p => p.Manufacturers
                                        .Select(t => new Manufacturer
                                                         {
                                                             ManufacturerID = t.ManufacturerID,
                                                             ManufacturerName = t.ManufacturerName})
                                        .AsEnumerable());