Find the best matching product version from a list of available product versions

Millar

How to return the best matching/next available product versionId from a list of available product versions ?

Here is the logic based on the sample data in the table

Look for the best matching version available less than 10.10.20 and should return its versionID eg1:GetVersion("10.10.20") should return 5 ( because in table there is no "10,10,20" major.minor.build combination available ,so it should look for the best matching version here the next available version is 10.7.1 ie., versionID 5

eg2:GetVersion("7.0.0") should return 3 ( because in table there is no "7,0,0" major.minor.build combination available ,so it should look for next available matching version .here the next available version is 6.2.1 ie., versionID 3

eg3:GetVersion("7.5.1") should return 4 ,here exact match is available soit should return versionid 4

 [Serializable]
    public class ProductVersions
    {
        [Key]
        public int Version_Id { get; set; }
        public int Major { get; set; }
        public int Minor { get; set; }
        public int Build { get; set; }
    }

Here is some sample data in my ProductVersions Table

    [version_id , Major,Minor,Build]
        1           3      0    1
        2           4     10    5
        3           6     2     1
        4           7     5     1
        5           10    7     1
        6           11    10   10

Here is my method that is expected to return best available product version

private int GetVersion(string versionNumber)
   {
    int version-id=0;

    version-id= //retrieve best matching version

     return version-id
    }
sloth

You can use the build-in Version class, since it already implements the <= operator you are basically looking for, and also can handle the string parsing for you:

var data = new List<Version>()
{
     new Version(3,0,1),
     new Version(4,10,5),
     new Version(6,2,1),
     new Version(7,5,1),
     new Version(10,7,1),
     new Version(11,10,10)
};

var case1 = new Version("10.10.20");
// match1 is 5; the index of a List is 0-based, so we add 1
var match1 = data.FindLastIndex(d => d <= case1) + 1;

var case2 = new Version("7.0.0");
// match2 is 3
var match2 = data.FindLastIndex(d => d <= case2) + 1;

var case3 = new Version("7.5.1");
// match3 is 4
var match3 = data.FindLastIndex(d => d <= case3) + 1;

It should be trivial to convert your sequence of ProductVersions to a list of Version objects.

If you don't want to use the Version class for whatever reason, you can implement the <= (and all other missing operators) yourself:

public class ProductVersions
{
   //TODO error checking
   public int Version_Id { get; set; }
   public int Major { get; set; }
   public int Minor { get; set; }
   public int Build { get; set; }

   public ProductVersions(int major, int minor, int build)
   {
        Major=major;
        Minor=minor;
        Build=build;
   }

   public ProductVersions(string version)
   {
        var tmp = version.Split('.');
        Major = Int32.Parse(tmp[0]);
        Minor = Int32.Parse(tmp[1]);
        Build = Int32.Parse(tmp[2]);
   }

   public static bool operator == (ProductVersions a, ProductVersions b)
   {
        return a.Major==b.Major && a.Minor==b.Minor && a.Build==b.Build;
   }

   public static bool operator != (ProductVersions a, ProductVersions b)
   {
        return !(a==b);
   }

   public static bool operator <= (ProductVersions a, ProductVersions b)
   {
        if (a == b)
            return true;
        return a < b;
   }

   public static bool operator >= (ProductVersions a, ProductVersions b)
   {
        if (a == b)
            return true;
        return a > b;
   }

   public static bool operator < (ProductVersions a, ProductVersions b)
   {
        if(a.Major==b.Major)
            if(a.Minor==b.Minor)
                return a.Build < b.Build;
            else
                return a.Minor < b.Minor;
        else
            return a.Major < b.Major;
   }

   public static bool operator > (ProductVersions a, ProductVersions b)
   {
        if(a.Major==b.Major)
            if(a.Minor==b.Minor)
                return a.Build > b.Build;
            else
                return a.Minor > b.Minor;
        else
            return a.Major > b.Major;
   }

And a simple test:

var data = new List<ProductVersions>()
{
     new ProductVersions(3,0,1)    { Version_Id = 1},
     new ProductVersions(4,10,5)   { Version_Id = 2},
     new ProductVersions(6,2,1)    { Version_Id = 3},
     new ProductVersions(7,5,1)    { Version_Id = 4},
     new ProductVersions(10,7,1)   { Version_Id = 5},
     new ProductVersions(11,10,10) { Version_Id = 6}
};

// ensure data is sorted by version
data.Sort((a,b) => a > b ? 1 : a < b ? -1 : 0);

var case1 = new ProductVersions("10.10.20");
// match1 is 5
var match1 = data.Last(d => d <= case1).Version_Id;

var case2 = new ProductVersions("7.0.0");
// match2 is 3
var match2 = data.Last(d => d <= case2).Version_Id;

var case3 = new ProductVersions("7.5.1");
// match3 is 4
var match3 = data.Last(d => d <= case3).Version_Id;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find the best matching product version from a list of available product versions

From Dev

How to check which version of a product is available in repositories for different ubuntu versions?

From Dev

Find all permutations(cartesian product) from a list<list<int>> in java

From Dev

Find all permutations(cartesian product) from a list<list<int>> in java

From Dev

Python itertools: Best way to unpack product of product of list of lists

From Dev

Not matching product names in the Array List in Java

From Dev

docker and product versions

From Dev

Maven find the list of available versions for an artifact

From Dev

How to find the maximum product of two elements in a list?

From Dev

How to check an installed version of a product from MSI

From Dev

How to check an installed version of a product from MSI

From Dev

pandas: find the first shipped product from a DataFrame

From Dev

Find Cost from Product ID in MS Access

From Dev

pandas: find the first shipped product from a DataFrame

From Dev

Retrieve info from additional field to product list

From Dev

Shopify - Get list of product from a specific collection

From Dev

Show products from a specific category in the product list

From Dev

Retrieve info from additional field to product list

From Dev

How to remove Configurable Product from cart list

From Dev

Woocommerce - Remove the available product inventory number from the shop page

From Java

Homebrew: list available versions with new formula@version format

From Dev

How to find out which windows version my product key matches?

From Dev

Comparing and matching product names from different stores/suppliers

From Dev

Multidimensional version of "kron" product?

From Dev

How to retrieve Product Version?

From Dev

Magento: Find Group Product of Simple Product

From Dev

Product Search Algorithm Best practice

From Dev

Prestashop 1.7 product list, product view changes

From Dev

Haskell product of list

Related Related

  1. 1

    Find the best matching product version from a list of available product versions

  2. 2

    How to check which version of a product is available in repositories for different ubuntu versions?

  3. 3

    Find all permutations(cartesian product) from a list<list<int>> in java

  4. 4

    Find all permutations(cartesian product) from a list<list<int>> in java

  5. 5

    Python itertools: Best way to unpack product of product of list of lists

  6. 6

    Not matching product names in the Array List in Java

  7. 7

    docker and product versions

  8. 8

    Maven find the list of available versions for an artifact

  9. 9

    How to find the maximum product of two elements in a list?

  10. 10

    How to check an installed version of a product from MSI

  11. 11

    How to check an installed version of a product from MSI

  12. 12

    pandas: find the first shipped product from a DataFrame

  13. 13

    Find Cost from Product ID in MS Access

  14. 14

    pandas: find the first shipped product from a DataFrame

  15. 15

    Retrieve info from additional field to product list

  16. 16

    Shopify - Get list of product from a specific collection

  17. 17

    Show products from a specific category in the product list

  18. 18

    Retrieve info from additional field to product list

  19. 19

    How to remove Configurable Product from cart list

  20. 20

    Woocommerce - Remove the available product inventory number from the shop page

  21. 21

    Homebrew: list available versions with new formula@version format

  22. 22

    How to find out which windows version my product key matches?

  23. 23

    Comparing and matching product names from different stores/suppliers

  24. 24

    Multidimensional version of "kron" product?

  25. 25

    How to retrieve Product Version?

  26. 26

    Magento: Find Group Product of Simple Product

  27. 27

    Product Search Algorithm Best practice

  28. 28

    Prestashop 1.7 product list, product view changes

  29. 29

    Haskell product of list

HotTag

Archive