为什么 .Equals 在此 LINQ 示例中不起作用?

发布于 2024-08-10 05:03:45 字数 268 浏览 11 评论 0原文

为什么这会产生一个空集?

Object[] types = {23, 234, "hello", "test", true, 23};

var newTypes = types.Select(x => x.GetType().Name)
    .Where(x => x.GetType().Name.Equals("Int32"))
    .OrderBy(x => x);

newTypes.Dump();

Why does this yield an empty set?

Object[] types = {23, 234, "hello", "test", true, 23};

var newTypes = types.Select(x => x.GetType().Name)
    .Where(x => x.GetType().Name.Equals("Int32"))
    .OrderBy(x => x);

newTypes.Dump();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萌能量女王 2024-08-17 05:03:46

LINQ 的问题是你必须停止用 SQL 术语来思考。在 SQL 中,我们是这样思考的:-

SELECT Stuff
FROM StufF
WHERE Stuff
ORDER BY Stuff

这就是你的代码的样子。然而在 LINQ 中我们需要这样思考:-

FROM Stuff
WHERE Stuff
SELECT Stuff
ORDER BY Stuff

The thing with LINQ is you've got to stop thinking in SQL terms. In SQL we think like this:-

SELECT Stuff
FROM StufF
WHERE Stuff
ORDER BY Stuff

That is what your code looks like. However in LINQ we need to think like this :-

FROM Stuff
WHERE Stuff
SELECT Stuff
ORDER BY Stuff
天赋异禀 2024-08-17 05:03:46
var newTypes = types.Select(x => x.GetType().Name)
    .Where(x => x.Equals("Int32"))
    .OrderBy(x => x);
var newTypes = types.Select(x => x.GetType().Name)
    .Where(x => x.Equals("Int32"))
    .OrderBy(x => x);
与往事干杯 2024-08-17 05:03:46

这不起作用,因为 Select 语句会将集合中的每个值转换为该值的基础类型的名称。生成的集合将仅包含字符串值,因此它们不会具有 Int32 名称。

This doesn't work because the Select statement will convert every value in the collection to the name of the underlying type of that value. The resulting collection will contain only string values and hence they won't ever have the name Int32.

魂归处 2024-08-17 05:03:45

当您进行选择时,您将获得一个 IEnumerable。然后,您将获取列表中每个字符串的类型(全部为“String”),并过滤掉它们不等于“Int32”(整个列表)的地方。因此...列表是空的。

When you do your select you're getting an IEnumerable<String>. Then you're taking the types of each string in the list (which is all "String") and filtering them out where they aren't equal to "Int32" (which is the entire list). Ergo...the list is empty.

贱人配狗天长地久 2024-08-17 05:03:45

等于工作得很好,这是你的查询不正确。如果要选择列表中的整数,请使用:

var newTypes = types.Where( x => x.GetType().Name.Equals("Int32") )
                    .OrderBy( x => x );

Equals works just fine, it's your query that isn't correct. If you want to select the integers in the list use:

var newTypes = types.Where( x => x.GetType().Name.Equals("Int32") )
                    .OrderBy( x => x );
无远思近则忧 2024-08-17 05:03:45

反转操作的顺序:(

var newTypes = types.Where(x => x is int)
    .OrderBy(x => x)
    .Select(x => x.GetType().Name);

请注意,这也使用直接类型检查,而不是相当奇特的 .GetType().Name.Equals(…))。

Reverse the order of the operations:

var newTypes = types.Where(x => x is int)
    .OrderBy(x => x)
    .Select(x => x.GetType().Name);

(Notice this also uses a direct type check instead of the rather peculiar .GetType().Name.Equals(…)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文