簡體   English   中英

在C#中使用IEnumerable和List的Groupby錯誤

[英]Groupby error using IEnumerable and List in c#

我正在實現一個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;
    }
}

我想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”字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM