在C#中使用IEnumerable和List的Groupby错误

时间:2019-03-02 18:33:39

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

我正在实现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;
    }
}

1 个答案:

答案 0 :(得分:0)

我猜在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();

在这种情况下,您不需要分组依据,并且上面的行可以简化为:

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

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