difficulty making Foreach lambda in a query

Asked

Viewed 73 times

0

I have this query

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)
            .AsNoTracking()
            .SelectMany(final => final.Categories,
            (final, c) => new CatalogItemResponse
            {
                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();

Before this lambda I had a Getall() and then I would foreach and take this line the way it is

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

Well, what I need is to take that same value, but assigning it within qry.Foreach and I’m not sure how to do it. I started trying it that way

 qry.ForEach(prd => prd.PictureFilename = ??????)

That’s right, that’s the way?

EDIT1

I did so by putting the Picture class in Left Join, but what do I assign in Select new? I gave an anonymous select new, what do I do? Any value I try to set, gives error at the bottom

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

            .GroupJoin(_pictureRepository.Table,
            prd => prd.Product.PictureId,
            pic => pic.Id,
            (prd, pic) => new { Product = prd.Product, Pictures = pic.DefaultIfEmpty()})
            .SelectMany(sel => sel.Pictures,
            (sel, pic) => new
            {

            })

            .Where(hdg => hdg.Product.Hidden == false)            
            .SelectMany(final => final.Categories,
            (final, c) => new CatalogItemResponse
            {
                ChildrenCategoryId = final.Product.ChildrenCategoryId,
                //atribuições.................

Include that

GroupJoin(_pictureRepository.Table,
                prd => prd.Product.PictureId,
                pic => pic.Id,
                (prd, pic) => new { Product = prd.Product, Pictures = pic.DefaultIfEmpty()})
                .SelectMany(sel => sel.Pictures,
                (sel, pic) => new
                {

                })

See this screenshot of the error on top of that line: .Where(hdg => hdg.Product.Hidden == false) inserir a descrição da imagem aqui

1 answer

2

In fact the correct syntax would be ForEach():

qry.ForEach(prd => {
    //DoSomething
});

See if it fits your problem:

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)
          .AsNoTracking()
          .ToList()
          .ForEach(PropertyDescriptor => {
            //DoSomething
          });

var listCatalogItems = qry
        .SelectMany(final => final.Categories,
        (final, c) => new CatalogItemResponse
        {
          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();

  • Victor, I tried to do this, but the problem is I can’t get Product on Foreach anymore and I don’t know how to add. In qry I have only Catalogitemresponse

  • And if you do the Foreach() before changing your list to Catalogitemresponse, it would meet?

  • And how would I do that? In what way, see that qry is the main query, at the moment I would take this information?

  • See the idea I changed in the topic, I created a list just to store the information and make the changes and then I create my Catalogitemresponse list from the changed data

  • Makes a mistake: 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)
 .AsNoTracking()
 .ToList()
 .ForEach(PropertyDescriptor => {
 //DoSomething
 }); This block explodes this error: It is not possible to assign void to an implicit type variable

  • Are you using the Propertydescriptor as a variable in foreach()? If it is, it will actually give an Exception, because there you must declare a variable corresponding to the current Foreach() item and not a class like Propertydescriptor

  • .Foreach(nameVariable => { //Dosomething })

  • Dude, I’m having trouble running your code above. It’s not working, it gives the error in the comment above. You know what might be causing this?

Show 3 more comments

Browser other questions tagged

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