1

我正在尝试使用 LINQ 外部联接的结果填充模型,但出现以下错误:无法将匿名 IEnemerable 隐式转换为列表。

我有以下模型:

public class adminEditProductsPricelistProductsVM
{
    public Product product { get; set; } // will be populated from Products table
    public PricelistProduct pricelistProduct { get; set; } // will be populated from PricelistProducts table
}

这是查询:

adminEditPricelistVM.adminEditProductsPricelistProductsVMs = 
          from product in products
          join pricelistProduct in pricelistProducts 
            on product.ProductId equals pricelistProduct.ProductId into gj
          from subpricelistProduct in gj.DefaultIfEmpty()
          select new  { product.Name };

请问这样做的正确方法是什么?

4

1 回答 1

1

您正在尝试将匿名类型的可枚举转换为adminEditProductsPricelistProductsVM不起作用的类型列表。您需要以下内容:

adminEditPricelistVM.adminEditProductsPricelistProductsVMs = 
                (from product in products
                 join pricelistProduct in pricelistProducts 
                   on product.ProductId equals pricelistProduct.ProductId into gj
                 from subpricelistProduct in gj.DefaultIfEmpty()
                 select new adminEditProductsPricelistProductsVM() 
                 {  
                     Product = product,
                     PricelistProduct = subpricelistProduct
                 }).ToList();
于 2013-05-12T21:05:29.213 回答