首页 > 解决方案 > 在 c# 中使用 IEnumerable 和 List 的 Groupby 错误

问题描述

我正在实现一个 api 控制器,需要返回按产品类型分组的列表。我对使用 IEnumerable vs List 感到困惑。groupby 要求 IEnumerable。目前,我在控制器方法的以下行中遇到语法错误。

missingProducts = missingProducts.GroupBy(a => a.ProductType);

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string, Manager.WebUI.ViewModels.Allocations.MissingProductsViewModel>>' to 'System.Collections.Generic.List<Manager.WebUI.ViewModels.Allocations.MissingProductsViewModel>'. An explicit conversion exists (are you missing a cast?)

我应该从 ApiController 基础中的方法返回 IEnumerable,然后在控制器方法中执行 ToList() 还是从 ApiController 基础返回列表,然后将返回类型从控制器方法转换为 IEnumerable。

楷模

public class MISSING_PRODUCT 
{
    public int PRODUCT_ID { get; set; }
    public string PRODUCT_NAME { get; set; }
    public string PRODUCT_TYPE { get; set; }
}

public class MissingProductsViewModel
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductType { get; set; }
}

API 控制器基础

public class ApiControllerBase : ApiController
{
    public List<MISSING_PRODUCT> GetMissingProducts()
    {
       var missingProduct =  
                IoC.Resolve<IPackageService>()
                    .PackageGetList<FN_MISSING_PRODUCTS, MISSING_PRODUCT>(new FN_MISSING_PRODUCTS());
        return missingProduct;
    }
 }

控制器方法

public class AllocationsController : ApiControllerBase
{
    [HttpGet]
    [Route("api/Allocations/{id}")]
    public IHttpActionResult Details(int id, DateTime date)
    {
        var viewModel = GetAllocationsViewModel(id, date);
        if (viewModel == null) return NotFound(); 
        return Ok(viewModel);
    }

    private AllocationsViewModel GetAllocationsViewModel(int id, DateTime date)
    {
        var ms = GetStrategy(id);
        DateTime d = new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1);
        if (ms.FIRM_ID != null)
        {
            var missingProducts = Mapper.Map<List<MISSING_PRODUCT>, List<MissingProductsViewModel>>(GetMissingProducts());
            missingProducts = missingProducts.GroupBy(a => a.ProductType);
            var vm = new AllocationsViewModel
            {
                MissingProducts = missingProducts
            };

            return vm;
        }

        return null;
    }
}

标签: c#asp.net-web-api

解决方案


我想missingProducts会在通话List<MissingProductsViewModel>后的类型。Mapper.Map()不过,GroupBy会回来IEnumerable<IGrouping<string, MissingProductsViewModel>>的。

var missingProducts = Mapper.Map<List<MISSING_PRODUCT>, List<MissingProductsViewModel>>(GetMissingProducts());
missingProducts = missingProducts.GroupBy(a => a.ProductType);

将上面的第二行更改为:

missingProducts = missingProducts.GroupBy(a => a.ProductType).Select(b => b.ElementAt(0)).ToList();

您可能需要进行一些空值检查。

在汤姆的评论后编辑

下面的行应该按产品名称为您提供缺少的产品:

missingProducts = missingProducts.GroupBy(a => a.ProductType).Where(b => b.Key == "PRODUCT_TYPE").SelectMany(c => c).ToList();

在这种情况下,您不需要 group by,上面的行可以简化为:

missingProducts = missingProducts.Where(a => a.ProductType == "PRODUCT_TYPE").ToList();

并且不要忘记更新硬编码的“PRODUCT_TYPE”字符串。


推荐阅读