Load information inside the lambda and use in lambda foreach

Asked

Viewed 67 times

3

I created a property in the class CatalogItemResponse to use in a method, but Boss asked to take it. It turns out I need it to load another property and without it I’m not getting it. This is my Left:

var qry = _productRepository.Table.GroupJoin(_categoriesRepository.Table,
            p => p.CategoryId,
            c => c.Id,
            (p, c) => new { Product = p, Categories = c.DefaultIfEmpty() })
            .Where(hdg => hdg.Product.Hidden == false)

            .SelectMany(final => final.Categories,
            (final, c) => new CatalogItemResponse
            {
                ChildrenCategoryId = final.Product.ChildrenCategoryId,
                //DistributionCenterErpId = final.Product.DistributionCenterErpId,
                //Atribuições
                .......
             }).ToList();

This is the property I removed from the class: DistributionCenterErpId, being populated in the line commented on in Left. So in the lambda Foreach, I give a new Product(), and of course, I kill the values of Product, having to repopulate it again that way:

qry.ForEach(q =>
            {
                var product = new Product();

                product.CategoryId = q.CategoryId;
                product.AuthorizeMaximumPercentageAlteration = q.AuthorizeMaximumPercentageAlteration;
                product.AuthorizeMinimumPercentageAlteration = q.AuthorizeMinimumPercentageAlteration;
                product.HaveMaximumPercentage = q.HaveMaximumPercentage;
                product.HaveMinimumPercentage = q.HaveMinimumPercentage;
                product.Hidden = q.Hidden;
                product.ChildrenCategoryId = q.ChildrenCategoryId;
                product.Description = q.Description;
                //product.DistributionCenterErpId = q.DistributionCenterErpId;
                //Mais atribuições e etc
});

Notice that I commented on this line product.DistributionCenterErpId = q.DistributionCenterErpId 'Cause I don’t have the field anymore q.DistributionCenterErpId in class and it’s obviously going to fail at that point. How do I take the value of the field in Left and then assign(use) to Foreach by repopulating Product in that property? Here’s how?

EDIT1

I made an anonymous new, ok, I can put any property. However two problems. The return of the method is a List<CatalogItemResponse> and as I gave an anonymous, I lost this reference and do not know what to do to return and do not know how to bring Product in Foreach. I know q is anonymous guy who owns Product already filled, but I don’t know how to assign it. So it was lambda with the anonymous type and Return with error.

    public List<CatalogItemResponse> GetAllCatalog(int resellerId)
            {
                var qry = _productRepository.Table.GroupJoin(_categoriesRepository.Table,
                p => p.CategoryId,
                c => c.Id,
                (p, c) => new { Product = p, Categories = c.DefaultIfEmpty() })
                .Where(hdg => hdg.Product.Hidden == false)

                .SelectMany(final => final.Categories,
                (final, c) => new
                {
                    ChildrenCategoryId = final.Product.ChildrenCategoryId,

                    DolarRate = 0.0m,
                    ResellerPriceUSD = 0.0m,
                    ResellerPriceBRL = 0.0m,
                    BasePriceBRL = 0.0m,
                    BasePriceUSD = 0.0m,

                    CategoryId = final.Product.CategoryId,
                    CategoryName = (c != null ? c.Name : null),
                    PictureId = final.Product.PictureId,
                    Description = final.Product.Description,
                    ShortDescription = final.Product.ShortDescription,
                    Name = final.Product.Name,
                    NameHtml = string.IsNullOrEmpty(final.Product.NameHtml) ? final.Product.Name : final.Product.NameHtml,
                    PartNumber = final.Product.PartNumber,
                    Hidden = final.Product.Hidden,
                    Order = final.Product.Order,
                    HaveMaximumPercentage = final.Product.HaveMaximumPercentage,
                    MaximumPercentage = final.Product.MaximumPercentage,
                    HaveMinimumPercentage = final.Product.HaveMinimumPercentage,
                    MinimumPercentage = final.Product.MinimumPercentage,
                    AuthorizeMaximumPercentageAlteration = final.Product.AuthorizeMaximumPercentageAlteration,
                    AuthorizeMinimumPercentageAlteration = final.Product.AuthorizeMinimumPercentageAlteration,
                    StandardMarkup = final.Product.StandardMarkup,
                    DistributionCenterErpId = final.Product.DistributionCenterErpId,
                    PictureFilename = final.Product.Picture.FileName

                }).ToList();

                qry.ForEach(q =>
                {
                    var catalogo = new CatalogItemResponse();
                    var product = new Product();

                    catalogo.CategoryId = q.CategoryId;
                    catalogo.AuthorizeMaximumPercentageAlteration = q.AuthorizeMaximumPercentageAlteration;
                    catalogo.AuthorizeMinimumPercentageAlteration = q.AuthorizeMinimumPercentageAlteration;
                    catalogo.HaveMaximumPercentage = q.HaveMaximumPercentage;
                    catalogo.HaveMinimumPercentage = q.HaveMinimumPercentage;
                    catalogo.Hidden = q.Hidden;
                    catalogo.ChildrenCategoryId = q.ChildrenCategoryId;
                    catalogo.Description = q.Description;
                    //catalogo.DistributionCenterErpId = q.DistributionCenterErpId;
                    catalogo.MaximumPercentage = q.MaximumPercentage;
                    catalogo.MinimumPercentage = q.MinimumPercentage;
                    catalogo.Name = q.Name;
                    catalogo.NameHtml = q.NameHtml;
                    catalogo.Order = q.Order;
                    catalogo.PartNumber = q.PartNumber;
                    catalogo.PictureId = q.PictureId;
                    catalogo.ShortDescription = q.ShortDescription;
                    catalogo.StandardMarkup = q.StandardMarkup;  

                    var parentProducts = _productService.GetParentsOf(product.Id).Select(x => x.PartNumber);
                    catalogo.Parents.AddRange(parentProducts);

                    product.AuthorizeMaximumPercentageAlteration = q.AuthorizeMaximumPercentageAlteration;
                    product.AuthorizeMinimumPercentageAlteration = q.AuthorizeMinimumPercentageAlteration;

//Continua fazendo assim, propriedade a propriedade ou tem outra forma de preencher Product???

                    var price = _erpPriceService.GetPrice(product, 1, resellerId).Result;

                    if (price.BasePriceUSD > 0)
                        catalogo.DolarRate = price.BasePriceBRL / price.BasePriceUSD;

                    catalogo.ResellerPriceUSD = price.ResellerPriceUSD;
                    catalogo.ResellerPriceBRL = price.ResellerPriceBRL;
                    catalogo.BasePriceBRL = price.BasePriceBRL;
                    catalogo.BasePriceUSD = price.BasePriceUSD;

                });

                return qry;

EDIT2

I made another change to the query, so there is no need to reload Product in Foreach. The problem is that I don’t know how to pass a Product object in the method parameter, that method: var price = _erpPriceService.GetPrice(product, 1, resellerId).Result;. The new query looks like this

var qry = _productRepository.Table.GroupJoin(_categoriesRepository.Table,
            p => p.CategoryId,
            c => c.Id,
            (p, c) => new { Product = p, Categories = c.DefaultIfEmpty() })
            .Where(hdg => hdg.Product.Hidden == false)

            .SelectMany(final => final.Categories,
            (final, c) => new
            { final.Product, c }).ToList();

            var query = qry;

            qry.ForEach(q =>
            {
                var catalogo = new CatalogItemResponse();

                catalogo.CategoryId = q.Product.CategoryId;
                catalogo.AuthorizeMaximumPercentageAlteration = q.Product.AuthorizeMaximumPercentageAlteration;
                catalogo.AuthorizeMinimumPercentageAlteration = q.Product.AuthorizeMinimumPercentageAlteration;
                catalogo.HaveMaximumPercentage = q.Product.HaveMaximumPercentage;
                catalogo.HaveMinimumPercentage = q.Product.HaveMinimumPercentage;
                catalogo.Hidden = q.Product.Hidden;
                catalogo.ChildrenCategoryId = q.Product.ChildrenCategoryId;
                catalogo.Description = q.Product.Description;
                //catalogo.DistributionCenterErpId = q.DistributionCenterErpId;
                catalogo.MaximumPercentage = q.Product.MaximumPercentage;
                catalogo.MinimumPercentage = q.Product.MinimumPercentage;
                catalogo.Name = q.Product.Name;
                catalogo.NameHtml = q.Product.NameHtml;
                catalogo.Order = q.Product.Order;
                catalogo.PartNumber = q.Product.PartNumber;
                catalogo.PictureId = q.Product.PictureId;
                catalogo.ShortDescription = q.Product.ShortDescription;
                catalogo.StandardMarkup = q.Product.StandardMarkup;               

                var parentProducts = _productService.GetParentsOf(q.Product.Id).Select(x => x.PartNumber);
                catalogo.Parents.AddRange(parentProducts);               

                var price = _erpPriceService.GetPrice(product, 1, resellerId).Result;

                if (price.BasePriceUSD > 0)
                    catalogo.DolarRate = price.BasePriceBRL / price.BasePriceUSD;

                catalogo.ResellerPriceUSD = price.ResellerPriceUSD;
                catalogo.ResellerPriceBRL = price.ResellerPriceBRL;
                catalogo.BasePriceBRL = price.BasePriceBRL;
                catalogo.BasePriceUSD = price.BasePriceUSD;

            });

            return qry;

qry is no longer the Catalogitemresponse type and do not know how to assign a Catalogitemresponse in Return

EDIT3

At the beginning of the method I did this:

var listaResponse = new List<CatalogItemResponse>();

And within Foreach, after all the assignments, I did this

listaResponse.Add(catalogo);

And I returned this

return listaResponse;

If that’s what you should do, all that’s left to play Product in the ForEach and I don’t know how to do it

  • You can use reflection, or the Automapper

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.