如果它们的值等于C#,则比较请求中的属性

时间:2019-03-05 10:52:25

标签: c# dotnet-httpclient

我想比较请求中的属性是否具有相同的值。我有以下用于通过PostMan请求的json。

“request”:[ {
“Id”: “1234567”,
“Position”: “1”,
“IsSet”: true
},
{
“Id”: “1234587”,
“Position”: “1”,
“IsSet”: true
}, 
]

在代码中,我想比较属性Position和IsSet是否对请求中的每个id具有相同的值。如果他们不抛出错误。

public class Info
{
     public string Id {get; set;}
     public string Position {get; set;}
     public bool IsSet {get; set;}
}

我有一个称为Validate的方法来验证那些属性。

public class Validate(Info context)
{
    foreach (var item in context)
    {
        // what code should check this
    }
}

3 个答案:

答案 0 :(得分:4)

您可以为此使用LINQ SelectDistinct

这是示例"Validate"方法。

List<Test> objs = new List<Test>()
    {
        new Test(){ Position = "random position 1", IsSet = true, Id = 123 },
        new Test(){ Position = "random position 2", IsSet = true, Id = 123 },
        new Test(){ Position = "random position 3", IsSet = true, Id = 123 }

    };

    if(objs.Count() > 1){
        var query = objs.Select(p => new { p.Id, p.IsSet }).Distinct();

        var allTheSame = query.Count() == 1;

        Console.WriteLine(allTheSame);
    }else{

        Console.WriteLine("Nothing To Compare Against");    
    }
}

这里的逻辑是检查列表中是否有超过1个项目-只是我们知道有一些要与之比较的值。

如果有多个属性,请选择要与之匹配的属性,并在其上调用distinct。

然后我们获得不同值的计数,如果它们都匹配,我们将始终从query.Count()返回1,从而得到布尔检查。

在这一点上,如果allTheSamefalse,则可以抛出错误而不是Console.WriteLine

在第二个Console.WriteLine中,您始终可以返回true,因为没有什么可比较的,可以使它与众不同。

这是一个示例dotNetFiddle

答案 1 :(得分:2)

我喜欢Adriani6的回答。但是它仅适用于简单的类。我认为最好的解决方案是等于方法。您可以使用Resharper(Alt + insert,Equaliti成员)轻松生成它:

public class Info
    {
        protected bool Equals(Info other)
        {
            return string.Equals(Id, other.Id) && string.Equals(Position, other.Position) && IsSet == other.IsSet;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((Info) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var hashCode = (Id != null ? Id.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ (Position != null ? Position.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ IsSet.GetHashCode();
                return hashCode;
            }
        }

        public string Id { get; set; }
        public string Position { get; set; }
        public bool IsSet { get; set; }
    }

答案 2 :(得分:2)

您可以使用嵌套循环简单地遍历Validation方法中的列表。 假设它是IEnumerable(如数组或List),则可以执行以下操作:

// not a class, context is IEnumerable, not a single entitry
// Returns true if OK, false if any element is not the same.
// 'Sameness' (equality) defined in Info-class as implemented by IEquatable
public bool Validate(IEnumerable<Info> context)
{
    for (int i = 0; i < context.Count(); i++)
    {
        for (int j = i + 1; j < context.Count(); j++)
        {
            if (!context[i].Equals(context[j])) {return false;}
        }
    }
    return true;
}

和带有IEquatable

的信息
public class Info : IEquatable<Info>
{
    protected bool Equals(Info other)
    {
        return string.Equals(Id, other.Id) && string.Equals(Position, other.Position) && IsSet == other.IsSet;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((Info) obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            var hashCode = (Id != null ? Id.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ (Position != null ? Position.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ IsSet.GetHashCode();
            return hashCode;
        }
    }

    public string Id { get; set; }
    public string Position { get; set; }
    public bool IsSet { get; set; }
}

如果想花哨的话,可以像这样重载==和!=运算符:

public static bool operator ==(Info lhs, Info rhs) { return lhs.Equals(rhs); }
public static bool operator !=(Info lhs, Info rhs) { return !(lhs == rhs); }