Add new values to an already filled Linq variable

Asked

Viewed 213 times

1

I made that Lambda/Linq:

var qry = _productRepository.Table.GroupJoin(_categoriesRepository.Table,
                p => p.CategoryId,
                c => c.Id,
                (p, c) => new { Product = p, Categories = c.DefaultIfEmpty() })
                .SelectMany(final => final.Categories,
                (final, c) => new CatalogItemResponse
                {
                    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

                }).ToList();

I did it to replace a Foreach, which every step was going to the bank, breaking the performance. Before I had the code below inside the Foreach and I could not put inside the expression above(qry):

if (product.PictureId.HasValue)
            item.PictureFilename = product.Picture.FileName;

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

        item.Parents.AddRange(parentProducts);

        var price = await _erpPriceService.GetPrice(product, 1, resellerId);
        if (product.PriceType == ProductPriceType.Dolar)
        {
            if (price.BasePriceUSD > 0)
                item.DolarRate = price.BasePriceBRL / price.BasePriceUSD;
        }

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

item was a variable within the foreach of type Catalogitemresponse. Well, I now need to load these values, in the above conditions into the var qry for when I do the commands below, these values will already be loaded on the screen

var model = new CatalogResponse();
model.CatalogDate = DateTime.UtcNow;
model.Items = new List<CatalogItemResponse>( qry );
return Ok(model);

How do I do that? It takes a foreach of the kind:

qry.ForEach(q => q.meu_campo_aqui_nas_condições....)?

OBS: product was the Foreach variable, which covered a products list.

EDIT

I did it on assignment:

qry.ForEach(q => {
     var product = new Product();
     var price = _erpPriceService.GetPrice(product, 1, resellerId);
     if (price.BasePriceUSD > 0)
           q.DolarRate = price.BasePriceBRL / price.BasePriceUSD;
});

But price does not have Basepriceusd and the rest

It worked that way(with async):

qry.ForEach(async q => {
                var product = new Product();
                var price = await _erpPriceService.GetPrice(product, 1, resellerId);
                if (price.BasePriceUSD > 0)
                    q.DolarRate = price.BasePriceBRL / price.BasePriceUSD;
            });

EDIT2

Doing so?

qry.ForEach(async q => {
                var product = new Product();
                var price = await _erpPriceService.GetPrice(product, 1, resellerId);
                if (price.BasePriceUSD > 0)
                    q.DolarRate = price.BasePriceBRL / price.BasePriceUSD;

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

            });

The values continue to come 0.00. I believe the new Product(), may have killed. Before I had a service that gave a Getall() in Product. I killed her because I brought everything(Getall()). I’ll put it back in place of new Product() and see if I can return the amounts of R$ and Dollar.

  • I did not understand the down vote above, since the question only allows an answer, that is, how to assign values to an already filled var lambda

  • 2

    I’m even used to this sort of thing... must have some citizens who were born to give down vote or ask to close questions that are within the rules.

  • Lack is moderation for that. I’ve had some discussions here. A guy took it to the professional side and always answers me well, but there is another(s) that no matter the question, if it is mine, I take downvote face.

  • 2

    What exactly is your question? How to store the price into your query?

  • That’s right, the price on the previous foreach came from a service, as seen in the code. As it was inside a foreach, I passed the foreach var() product, quantity 1 and reseller or user ID.

  • I tried that: qry.ForEach(q => q.ResellerPriceUSD = _erpPriceService.GetPrice(??????, 1, resellerId));, but where is the question, if you expect a Product and var lambda q is not a product. I do not know what to do

  • It doesn’t seem very practical... the way you are you’d have to requisition the method for each property

  • 1

    It is not the case to use a stored Procedure?

  • 1

    @Augustovasques, the system already works. I just have to improve his performance, this is my job.

Show 4 more comments
No answers

Browser other questions tagged

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