我想在LINQ中做相同的以下内容,但我无法弄清楚如何:
IEnumerable<Item> items = GetItems();
items.ForEach(i => i.DoStuff());
Run Code Online (Sandbox Code Playgroud)
什么是真正的语法?
我最近开始使用LINQ并且令人惊叹.我想知道LINQ是否允许我将函数 - 任何函数 - 应用于集合的所有元素,而不使用foreach.像python lambda函数之类的东西.
例如,如果我有一个int列表,我可以使用LINQ为每个元素添加一个常量
如果我有一个数据库表,我可以使用LINQ为所有记录设置一个字段.
我正在使用C#
有人可以解释为什么以下LINQ查询抛出InvalidOperationException?
(不要说列表中没有元素,我正在寻找的值总是存在于集合中)
class Program
{
static int lastNumber;
static void Main()
{
int loopCount = 100; int minValue = 1; int maxValue = 10000;
var numbers = Enumerable.Range(minValue, maxValue).ToList();//or ToArray();
Random random = new Random();
for (int i = 0; i < loopCount; i++)
{
//.First() throws the exception but it is obvious that the value exists in the list
int x = numbers.Where(v => v == NewMethod(minValue, maxValue, random)).First();
}
Console.WriteLine("Finished");
Console.ReadLine();
}
private static int NewMethod(int minValue, int …Run Code Online (Sandbox Code Playgroud) 我有以下内容:
foreach (var objectiveDetail in add)
{
_uow.ObjectiveDetails.Add(objectiveDetail);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在LINQ中做到这一点.