Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C语言的组合中找到最佳匹配#_C#_Collections_Combinations_Ienumerable_Iequalitycomparer - Fatal编程技术网

C# 如何在C语言的组合中找到最佳匹配#

C# 如何在C语言的组合中找到最佳匹配#,c#,collections,combinations,ienumerable,iequalitycomparer,C#,Collections,Combinations,Ienumerable,Iequalitycomparer,假设我有一个集合,它有5个属性 我的意见: A=1;B=2;c=3;d=4 使用这个输入,我必须得到属性E(它与我的输入非常接近或接近我的输入) 在这种情况下,我的预期结果是 结果A和结果C 因此,您应该在代码中检查以下组合 if(A and B and C and D) all matches collection take that; break; else if(A and B and C) matches the collection take that;

假设我有一个集合,它有5个属性

我的意见: A=1;B=2;c=3;d=4

使用这个输入,我必须得到属性E(它与我的输入非常接近或接近我的输入)

在这种情况下,我的预期结果是

结果A和结果C

因此,您应该在代码中检查以下组合

if(A and B and C and D) all matches collection 
    take that;
    break;
else if(A and B and C) matches the collection
    take that;
    break;
else if (A and B and D) matches the collection 
    take that;
    break;
else if (A and B and D) matches the collection 
    take that;
else if (A and B) matches the collection 
    take that;
    break;
else if(A and C) matches the collection
    take that;
    break;
else if(A and D) matches the collection
    take that;
    break;
else if A matches the collection
    takethat;
    break;
else if B matches the collection
    take that;
    break;
else if c matches the collection
    take that;
    break;
else if D matches the collection
    take that;
    break;
else
    "No Match Found"
因此,当要检查的属性数量越多,我们需要构建的组合就越多。因此,我需要创建一个实用程序,动态构建组合并检查对象的比较。我可以将属性作为字符串数组传递,并可以进行所需的组合,但我不知道如何访问对象属性。

如何处理这种情况?

您可以在类中定义自定义函数,如:

public class CustomObject
{
    public CustomObject(string p1, string p2, string p3, string p4)
        : this(p1,p2,p3,p4,null)
    {
    }

    public CustomObject(string p1, string p2, string p3, string p4, string p5)
    {
        Prop1 = p1;
        Prop2 = p2;
        Prop3 = p3;
        Prop4 = p4;
        Prop5 = p5;
    }

    public string Prop1 {get;set;}
    public string Prop2 {get;set;}
    public string Prop3 {get;set;}
    public string Prop4 {get;set;}
    public string Prop5 {get;set;}

    public int NumberOfSameProps(CustomObject other)
    {
        return (Prop1 == other.Prop1 ? 1 : 0) +
               (Prop2 == other.Prop2 ? 1 : 0) +
               (Prop3 == other.Prop3 ? 1 : 0) +
               (Prop4 == other.Prop4 ? 1 : 0);
    }
}
然后你所要做的就是得到比较返回最大值的项目

用法:

CustomObject obj1 = new CustomObject("1","2","3",null,"Result A");
CustomObject comp = new CustomObject("1","2","3","4");
int nb = obj1.NumberOfSameProps(comp); // returns 3

这堵巨大的文字墙很难阅读-你能把它的格式设置得更好一点吗?从学校的作业中复制并粘贴当你在第一面墙上写下sudo代码时,你只是在寻找答案吗?你想让我们告诉你,你可能想制作一个对象来存储你的检查结果,然后你可以说object.A object.B等等?