比较请求中的属性是否具有相等的值 C#
Compare properties in request if they have equal value C#
我想比较请求中的属性是否具有相同的值。我有以下 json 用于通过 PostMan 请求。
“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
}
}
您可以为此目的使用 LINQ Select
和 Distinct
。
这是一个示例 "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 项 - 只是为了让我们知道有一些值可以与之进行比较。
如果不止一个,select 您想要匹配对象并对其调用 distinct 的属性。
然后我们得到不同值的计数,如果它们都匹配,我们将始终从 query.Count()
中得到 1 returned,因此进行布尔检查。
此时如果 allTheSame
是 false
你可以抛出你的错误而不是 Console.WriteLine
在第二个 Console.WriteLine
中,您总是可以 return 正确,因为没有什么可比较的,因此它足够独特。
这是一个示例 dotNetFiddle。
我喜欢 Adriani6 的回答。但它只适用于简单的 类。我认为最好的解决方案是 Equals 方法。您可以使用 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; }
}
您可以使用嵌套循环在您的验证方法中简单地遍历您的列表。
假设它是一个类似于数组或列表的 IEnumerable,您可以执行以下操作:
// 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); }
我想比较请求中的属性是否具有相同的值。我有以下 json 用于通过 PostMan 请求。
“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
}
}
您可以为此目的使用 LINQ Select
和 Distinct
。
这是一个示例 "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 项 - 只是为了让我们知道有一些值可以与之进行比较。
如果不止一个,select 您想要匹配对象并对其调用 distinct 的属性。
然后我们得到不同值的计数,如果它们都匹配,我们将始终从 query.Count()
中得到 1 returned,因此进行布尔检查。
此时如果 allTheSame
是 false
你可以抛出你的错误而不是 Console.WriteLine
在第二个 Console.WriteLine
中,您总是可以 return 正确,因为没有什么可比较的,因此它足够独特。
这是一个示例 dotNetFiddle。
我喜欢 Adriani6 的回答。但它只适用于简单的 类。我认为最好的解决方案是 Equals 方法。您可以使用 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; }
}
您可以使用嵌套循环在您的验证方法中简单地遍历您的列表。 假设它是一个类似于数组或列表的 IEnumerable,您可以执行以下操作:
// 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); }