Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Damian Green 452 posts 1433 karma points
    Jun 01, 2012 @ 00:04
    Damian Green
    0

    LINQ to get the cheapest variant price

    Hi,

    Im not a heavy user of LINQ and as im still quite new to UC too im having trouble with what ought to be a simple query.

    I have a product with several variants and i want to get the cheapest variant price for that product.

    Here is what i have but i know its not right... I am working from the current product:

     var x = from v in ucProduct.Variants

                           var x = select v.PriceGroupPrices.Min(p => p.Price).Value;

                    return x.Single();   

    I can the output From: £xxx on my page.

    TIA

  • Damian Green 452 posts 1433 karma points
    Jun 01, 2012 @ 00:09
    Damian Green
    0

    Arghh! Pasted the wrong code and forum wont let me edit! :(

    Here it is again:

     var x = from v in ucProduct.Variants

     select v.PriceGroupPrices.Min(p => p.Price).Value;

    return x.Single();   

    Once i have the value I can then output

    From: £xxx on my page.

    TIA

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jun 01, 2012 @ 14:39
    Søren Spelling Lund
    0

    One thing to keep in mind is that the lowest value amount might not mean that it's also the lowest price if you're working with multiple currency.

    Take this example:

    Variant priced at USD 1, EUR 1, and GBP 1. Which is the lowest? :)

    Here's a quick bit of code that just grabs the lowest price based on the value without taking currency into account:

    // Grab any product with variants
    Product product = Product.All().First(x => x.ParentProduct == null);
    // Find the cheapest price among all prices assigned to indivudla variants and then
    // find the variant with the lowest overall price.
    decimal? cheapestVariantPrice = product.Variants.Min(x => x.PriceGroupPrices.Min(y => y.Price)); foreach (var variant in product.Variants) { foreach (var price in variant.PriceGroupPrices) { Console.WriteLine("Variant {0} priced {1} in {2}".FormatWith(variant.VariantSku, price.Price, price.PriceGroup.Name)); } } Console.WriteLine("Cheapest variant priced {0}".FormatWith(cheapestVariantPrice));
  • Damian Green 452 posts 1433 karma points
    Jun 01, 2012 @ 15:34
    Damian Green
    0

    Ah i wasnt a million miles off then.

    Im making my online store deal only with GBP as thats what we will be selling in.  Is this a mistake? 

     

Please Sign in or register to post replies

Write your reply to:

Draft