locked
How to concat 2 lists RRS feed

  • Question

  • User-1104215994 posted

    Hello,

    I am using EF 6. I am querying a table as follows:

    //Query GameBank database
                    var gameBankProductListResult = await _unitOfWork.GameBankRepository.GetGameBankProducts(
                        x => x.used == 0,
                        g => new {g.productCode, g.productDescription, g.unitPrice}, gcs => new
                        {
                            ProductID = gcs.Key.productCode,
                            ProductName = gcs.Key.productDescription,
                            Price = gcs.Key.unitPrice,
                            StockQuantity = gcs.Sum(g => g.quantity),
                            IsStockAvailable = gcs.Sum(g => g.quantity) > 0
                        });

    And getting a response from web API as follows:

    response = await Utilities.CallRazer(products, "Product/");
    
                    var htmlResponse = await response.Content.ReadAsStringAsync();
                    var model = JsonConvert.DeserializeObject<ProductResponseDto>(htmlResponse);
    
                    var productsListRazer = model.Products.Select(gcs =>
                            new
                            {
                                gcs.ProductID,
                                gcs.ProductName,
                                gcs.Price,
                                gcs.StockQuantity,
                                gcs.IsStockAvailable
                            })
                        .ToList();

    I wonder if there is a way to <g class="gr_ gr_208 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="208" data-gr-id="208">concat</g> 2 lists? I tried this but it says the type arguments for method IEnum <g class="gr_ gr_380 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="380" data-gr-id="380">concat</g> can not be inferred from usage. Try specifying the type arguments explicitly.

    var merged = productsListRazer.Concat(productsListRazer)
                        .GroupBy(g => new {g.ProductID, g.ProductName, g.Price, g.IsStockAvailable})
                        .Select(gcs => new
                        {
                            gcs.First().ProductID,
                            gcs.Key.ProductName,
                            gcs.Key.Price,
                            StockQuantity = gcs.Sum(g => g.StockQuantity),
                            gcs.Key.IsStockAvailable
                        })
                        .ToList();

    Friday, July 26, 2019 8:20 PM

Answers

  • User303363814 posted

    Every anonymous type is different.  So the type of gamesBankProductListResult is different from productsListRazer

    To fix, you just need to create a type (class) and use it in both select clauses, then your concat will be fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 28, 2019 7:06 AM

All replies

  • User-943250815 posted

    Since both lists have share same Type you can try Union like

    var FinalList = (myList1.Union(myList2)).Tolist

    Friday, July 26, 2019 8:46 PM
  • User-1104215994 posted

    I am getting List does not contain a definition for Union...

    Saturday, July 27, 2019 6:47 AM
  • User-1104215994 posted

    I think this query needs to be Enumerable;

    //Query GameBank database
                    var gameBankProductListResult = await _unitOfWork.GameBankRepository.GetGameBankProducts(
                        x => x.used == 0, g => new {g.productCode, g.productDescription, g.unitPrice},
                        gcs => new
                        {
                            ProductID = gcs.Key.productCode,
                            ProductName = gcs.Key.productDescription,
                            Price = gcs.Key.unitPrice,
                            StockQuantity = gcs.Sum(g => g.quantity),
                            IsStockAvailable = gcs.Sum(g => g.quantity) > 0
                        });
    
    
    
    
    ...
    
    
    
    public virtual async Task<List<T>> GetGameBankProducts<T, TType>(Expression<Func<TEntity, bool>> where,
                Expression<Func<TEntity, TType>> groupBy, Expression<Func<IGrouping<TType, TEntity>, T>> select)
            {
                return await dbSet.Where(where)
                    .GroupBy(groupBy)
                    .Select(select)
                    .ToListAsync();
            }

    Saturday, July 27, 2019 8:35 AM
  • User303363814 posted

    Are you really trying to concat the same list to itself as you have shown?  Why are you introducing an anonymous type?  model.Products seems to be what you are trying to make two copies of.  Is that right?

    Sunday, July 28, 2019 3:57 AM
  • User-1104215994 posted

    There was a typo.

    var merged = gameBankProductListResult.Concat(productsListRazer)
                        .GroupBy(g => new {g.ProductID, g.ProductName, g.Price, g.IsStockAvailable})
                        .Select(gcs => new
                        {
                            gcs.First().ProductID,
                            gcs.Key.ProductName,
                            gcs.Key.Price,
                            StockQuantity = gcs.Sum(g => g.StockQuantity),
                            gcs.Key.IsStockAvailable
                        })
                        .ToList();

    Sunday, July 28, 2019 6:22 AM
  • User303363814 posted

    Every anonymous type is different.  So the type of gamesBankProductListResult is different from productsListRazer

    To fix, you just need to create a type (class) and use it in both select clauses, then your concat will be fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 28, 2019 7:06 AM