Resolved How to concat 2 lists

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
361
Programming Experience
10+
Hello,


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

C#:
//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:
C#:
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 concat 2 lists? I tried this but it says the type arguments for method IEnum concat can not be inferred from usage. Try specifying the type arguments explicitly.
C#:
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();
 
I think this query needs to be Enumerable;

C#:
/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();
        }
 
Returning Product object for both queries solved my issue.

C#:
var gameBankProductListResult = await _unitOfWork.GameBankRepository.GetGameBankProducts(
                    x => x.used == 0, g => new {g.productCode, g.productDescription, g.unitPrice},
                    gcs => new Product
                    {
                        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
                    });
 
Back
Top Bottom