使用不区分大小写的比较从集合中减去记录 [英] Subtracting Records from a Set using case-insensitive comparison
本文介绍了使用不区分大小写的比较从集合中减去记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组记录:
type Person =
{
Name : string
Age : int
}
let oldPeople =
set [ { Name = "The Doctor"; Age = 1500 };
{ Name = "Yoda"; Age = 900 } ]
与上面的硬编码示例不同,数据集实际上来自数据源(对此我几乎没有控制权).现在,我需要从另一个数据源中减去一组数据.通常,第二个来源中的数据匹配,但偶尔在大小写上有所不同:
Unlike the hardcoded example above, the set of data actually comes from a data source (over which I have very little control). Now I need to subtract a set of data from another data source. In general, the data in this second source matches, but occasionally there is a difference in captialization:
let peopleWhoAreConfusedAboutTheirAge =
set [ { Name = "THE DOCTOR"; Age = 1500 } ]
当我尝试从第一组减去第二组时,由于字符串比较区分大小写,所以它失败了:
When I attempt to subtract the second set from the first, it fails because the string comparison is case sensitive:
let peopleWhoKnowHowOldTheyAre =
oldPeople - peopleWhoAreConfusedAboutTheirAge
val peopleWhoKnowHowOldTheyAre : Set<Person> =
set [{Name = "The Doctor";
Age = 1500;}; {Name = "Yoda";
Age = 900;}]
是否可以对People
记录的Name
字段执行不区分大小写的比较?
Is there a way to perform a case-insensitive comparison for the Name
field of the People
record?
推荐答案
这是另一种方法:
type Name(value) =
member val Value = value
override this.Equals(that) =
match that with
| :? Name as name -> StringComparer.CurrentCultureIgnoreCase.Equals(this.Value, name.Value)
| _ -> false
override this.GetHashCode() =
StringComparer.CurrentCultureIgnoreCase.GetHashCode(this.Value)
type Person =
{
Name: Name
Age: int
}
{Name=Name("John"); Age=21} = {Name=Name("john"); Age=21} //true
这篇关于使用不区分大小写的比较从集合中减去记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文