LINQ DISTINCT运算符,忽略大小写? [英] LINQ Distinct operator, ignore case?

查看:928
本文介绍了LINQ DISTINCT运算符,忽略大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下简单的例子:

    List<string> list = new List<string>() { "One", "Two", "Three", "three", "Four", "Five" };

    CaseInsensitiveComparer ignoreCaseComparer = new CaseInsensitiveComparer();

    var distinctList = list.Distinct(ignoreCaseComparer as IEqualityComparer<string>).ToList();



看样子CaseInsensitiveComparer没有实际使用做了区分大小写的比较。

It appears the CaseInsensitiveComparer is not actually being used to do a case-insensitive comparison.

在换句话说 distinctList 包含相同数量的项目作为列表即可。相反,我所期望的,例如,三和三被视为相等。

In other words distinctList contains the same number of items as list. Instead I would expect, for example, "Three" and "three" be considered equal.

我缺少的东西,或这是与DISTINCT运营商的问题?

Am I missing something or is this an issue with the Distinct operator?

推荐答案

StringComparer 做了你所需要的:

List<string> list = new List<string>() {
    "One", "Two", "Three", "three", "Four", "Five" };

var distinctList = list.Distinct(
    StringComparer.CurrentCultureIgnoreCase).ToList();



(或不变/序号的/ etc取决于你比较数据)

(or invariant / ordinal / etc depending on the data you are comparing)

这篇关于LINQ DISTINCT运算符,忽略大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆