相关疑难解决方法(0)

LINQ相当于foren for IEnumerable <T>

我想在LINQ中做相同的以下内容,但我无法弄清楚如何:

IEnumerable<Item> items = GetItems();
items.ForEach(i => i.DoStuff());
Run Code Online (Sandbox Code Playgroud)

什么是真正的语法?

linq ienumerable foreach

694
推荐指数
12
解决办法
46万
查看次数

为什么IEnumerable上没有ForEach扩展方法?

受到另一个询问失踪问题的启发 Zip功能的:

为什么课堂上没有ForEach扩展方法Enumerable?还是在任 唯一获得ForEach方法的类是List<>.有没有理由错过(表演)?

.net c# vb.net extension-methods

368
推荐指数
10
解决办法
14万
查看次数

为什么.ToList().Distinct()抛出错误但不抛出.Distinct().带有linq查询的ToList()

我无法知道LinqQuery.ToList().Distinct()LinqQuery.Distinct()之间的区别.ToList(); 对我来说两个看起来都一样

考虑以下示例代码:

List<string> stringList = new List<string>();

List<string> str1  = (from item in stringList
                                select item).ToList().Distinct();

List<string> str2 = (from item in stringList
                                 select item).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)

str1显示错误:"无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.Generic.List'.存在显式转换(您是否错过了转换?)"

但str2没有错误.

请帮助我理解这两者之间的差异.谢谢

c# linq

7
推荐指数
1
解决办法
6412
查看次数

标签 统计

c# ×2

linq ×2

.net ×1

extension-methods ×1

foreach ×1

ienumerable ×1

vb.net ×1