c# - 从可用产品版本列表中找到最匹配的产品版本

标签 c# entity-framework linq-to-entities

如何从可用产品版本列表中返回最匹配/下一个可用产品版本 ID?

下面是基于表中示例数据的逻辑

寻找低于 10.10.20 的最佳匹配版本,并应返回其版本 ID eg1:GetVersion("10.10.20") 应该返回 5(因为在表中没有可用的 "10,10,20"major.minor.build 组合,所以它应该寻找最匹配的版本 这里下一个可用版本是 10.7.1,即 versionID 5

eg2:GetVersion("7.0.0") 应该返回 3(因为在表中没有可用的 "7,0,0"major.minor.build 组合,所以它应该寻找下一个可用的匹配版本。这里 下一个可用版本是 6.2.1,即 versionID 3

eg3:GetVersion("7.5.1") 应该返回 4 ,这里可以使用完全匹配所以它应该返回 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; }
    }

这是我的 ProductVersions 表中的一些示例数据

    [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

这是我的方法,预计会返回最佳可用产品版本

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

    version-id= //retrieve best matching version

     return version-id
    }

最佳答案

您可以使用内置版本类,因为它已经实现了 <=您基本上正在寻找的运算符,也可以为您处理字符串解析:

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;

转换 ProductVersions 的序列应该很简单到 Version 的列表对象。

如果您不想使用 Version无论出于何种原因,您都可以实现 <= (以及所有其他缺失的运算符)你自己:

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;
   }

还有一个简单的测试:

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;

关于c# - 从可用产品版本列表中找到最匹配的产品版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18720296/

相关文章:

c# - 我可以将 WebSockets 用于桌面应用程序吗?

c# - EF 3.1 : Overcome LINQ GroupBy SQL translation problem

entity-framework - 防止 Entity Framework 在每个类型继承的表中添加重复的键

c# - 关于 IsNullOrWhiteSpace() 的快速提示中的 "String"与 "string"

c# - Entity Framework 通过包含对象实现多对多

c# - 如何更改 VS2015 中的代码生成策略?

LINQ 到 SQL。如何使用 .take() 选择所有记录

linq - 在 Linq 中调用 Select() 或 GroupBy() 是否会触发查询数据库?

database - 使用 LINQ to Entities 选择连续条目

c# - 为什么异步函数被调用两次?