how to do this using linq?

Possible Duplicate: LINQ equivalent of foreach for IEnumerable<T> please help me to figure out how to replace the loop below with a linq expression: using System.Web.UI.WebControls; ... Table table = ...; BulletedList list = new BulletedList(); foreach (TableRow r in table.Rows) { list.Items.Add(new ListItem(r.ToString())); } this is a contrived example, in reality i am not goin

如何使用linq做到这一点?

可能重复: IEnumerable <T>的foreach相当于LINQ 请帮我弄清楚如何用linq表达式替换下面的循环: using System.Web.UI.WebControls; ... Table table = ...; BulletedList list = new BulletedList(); foreach (TableRow r in table.Rows) { list.Items.Add(new ListItem(r.ToString())); } 这是一个人为的例子,实际上我不会将行转换为字符串。 我在问如何使用BulletedList.AddRange,并使用linq语句为它提

Select and ForEach on List<>

This question already has an answer here: LINQ equivalent of foreach for IEnumerable<T> 21 answers Let's start here: I am having a list of object. It's important to understand that, while accurate, that statement leaves ac# programmer wanting more. What kind of object? In the .Net world, it pays to always keep in mind what specific type of object you are working with. In

在列表<>上选择和ForEach

这个问题在这里已经有了答案: LINQ相当于foreach for IEnumerable <T> 21个答案 我们从这里开始: 我有一个对象列表。 理解这一点非常重要,尽管准确,但这种说法让ac#程序员想要更多。 什么样的对象? 在.Net世界中,始终牢记您正在使用的特定类型的对象是值得的。 在这种情况下,该类型是UserProfile 。 这可能看起来像是一个侧面问题,但它会很快与具体问题变得更相关。 你想说的是这样的: 我有一个U

Is there any method like ForEach for IList?

Possible Duplicate: LINQ equivalent of foreach for IEnumerable<T> List<T> has a method called ForEach which executes the passed action on each element of it. var names = new List<String>{ "Bruce", "Alfred", "Tim", "Richard" }; names.ForEach(p => { Console.WriteLine(p); }); But what if names is not a List<T> but an IList<T> , IList<T> doesn't have

有没有像ForEach for IList的方法?

可能重复: IEnumerable <T>的foreach相当于LINQ List<T>有一个名为ForEach的方法,它对每个元素执行传递的操作。 var names = new List<String>{ "Bruce", "Alfred", "Tim", "Richard" }; names.ForEach(p => { Console.WriteLine(p); }); 但是如果names不是List<T>而是IList<T> , IList<T>没有像ForEach这样的方法。 有其他选择吗? 使用foreach循环: foreach (var p in

call a function for each value in a generic c# collection

This question already has an answer here: Apply function to all elements of collection through LINQ [duplicate] 8 answers LINQ equivalent of foreach for IEnumerable<T> 21 answers LINQ doesn't help here, but you can use List<T>.ForEach : List<T>.ForEach Method Performs the specified action on each element of the List. Example: myList.ForEach(p => myFunc(p));

为泛型c#集合中的每个值调用一个函数

这个问题在这里已经有了答案: 通过LINQ将函数应用于所有收集元素[复制] 8个答案 LINQ相当于foreach for IEnumerable <T> 21个答案 LINQ在这里没有帮助,但你可以使用List<T>.ForEach : 列表<T> .ForEach方法 对List的每个元素执行指定的操作。 例: myList.ForEach(p => myFunc(p)); 传递给lambda表达式的值是列表项,因此在此示例中,如果myList是List<long>则p是一个long 。 正

Apply function to all elements of collection through LINQ

This question already has an answer here: LINQ equivalent of foreach for IEnumerable<T> 21 answers A common way to approach this is to add your own ForEach generic method on IEnumerable<T> . Here's the one we've got in MoreLINQ: public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { source.ThrowIfNull("source"); action.Thr

通过LINQ将函数应用于所有集合元素

这个问题在这里已经有了答案: LINQ相当于foreach for IEnumerable <T> 21个答案 解决这个问题的常用方法是在IEnumerable<T>上添加自己的ForEach泛型方法。 这是我们在MoreLINQ中获得的一个: public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { source.ThrowIfNull("source"); action.ThrowIfNull("action"); foreach (T element in source)

Linq style "For Each"

Possible Duplicate: Linq equivalent of foreach for IEnumerable Is there any Linq style syntax for "For each" operations? For instance, add values based on one collection to another, already existing one: IEnumerable<int> someValues = new List<int>() { 1, 2, 3 }; IList<int> list = new List<int>(); someValues.ForEach(x => list.Add(x + 1)); Instead of

Linq风格的“For Each”

可能重复: Linq相当于IEnumerable的foreach 是否有任何Linq风格的语法为“每个”操作? 例如,将基于一个集合的值添加到另一个已存在的集合: IEnumerable<int> someValues = new List<int>() { 1, 2, 3 }; IList<int> list = new List<int>(); someValues.ForEach(x => list.Add(x + 1)); 代替 foreach(int value in someValues) { list.Add(value + 1); } 使用ToList()扩展方法是最

c# how to get last time in foreach statement?

Possible Duplicate: Foreach loop, determine which is the last iteration of the loop foreach (DataRowView row in orderedTable.DefaultView) { if(lasttime) do-something; } orderedtable is a datatable does anyone know how to find out whether we are on the last foreach iteration? please keep in mind that i do have duplicates in orderedtable The foreach construct does not know such a thing

C#如何获得上次在foreach语句?

可能重复: Foreach循环,确定哪个是循环的最后一次迭代 foreach (DataRowView row in orderedTable.DefaultView) { if(lasttime) do-something; } orderedtable是一个数据表 有谁知道如何找出我们是否在最后的foreach迭代? 请记住,我确实有重复的顺序表 foreach构造不知道这样的事情,因为它同样适用于无界列表。 它无法知道什么是最后一项。 不过,您也可以重复手动方式: for (int i = 0; i < orderedTa

C# newbie: find out the index in a foreach block

I have a foreach block where I want to plot out for trace-debug purposes the index of the step inside the foreach. As a C# newbie I do it as follows: int i = 1; foreach (x in y) { ... do something ... WriteDebug("Step: "+i.ToString()); i++; } I wondered if there's any way to get the value of the current step's index without explicitly creating a variable for that purpose.

C#newbie:找出foreach块中的索引

我有一个foreach块,我想为trace-debug目的绘制foreach内部步骤的索引。 作为一个C#新手我这样做,如下所示: int i = 1; foreach (x in y) { ... do something ... WriteDebug("Step: "+i.ToString()); i++; } 我想知道是否有任何方法可以获取当前步骤索引的值,而无需为此目的明确创建变量。 编辑:澄清,我显然熟悉for循环的选项,但它不是我正在经历的数组,而是一个无序的集合。 编号的原因仅仅是为了

Getting the array key in a 'foreach' loop

How do I get the key of the current element in a foreach loop in C#? For example: PHP foreach ($array as $key => $value) { echo("$value is assigned to key: $key"); } What I'm trying to do in C#: int[] values = { 5, 14, 29, 49, 99, 150, 999 }; foreach (int val in values) { if(search <= val && !stop) { // Set key to a variable } } Grauenwolf's

在'foreach'循环中获取数组键

如何在C#中的foreach循环中获取当前元素的键? 例如: PHP foreach ($array as $key => $value) { echo("$value is assigned to key: $key"); } 我在C#中试图做的是: int[] values = { 5, 14, 29, 49, 99, 150, 999 }; foreach (int val in values) { if(search <= val && !stop) { // Set key to a variable } } Grauenwolf的方式是用数组完成这个最直接和最高效的方式:

Debugging a foreach loop in C#: what iteration is this?

Other than setting a debug variable and incrementing it every time you start the foreach, when you break in with the Visual Studio debugger connected, is there a way to tell that this is the Xth time through the loop? I guess this would be a feature of Visual Studio if anything, not something that would be added to the compiled code. Heres a previous Stack Overflow question that seems to be w

在C#中调试foreach循环:这是什么迭代?

除了设置一个调试变量并在每次启动foreach时增加它,当您与Visual Studio调试器连接时,有没有办法告诉这是循环中的第X个时间? 我想这将是Visual Studio的一个功能,如果有的话,而不是将被添加到编译代码的东西。 下面是一个以前的堆栈溢出问题,这似乎是你要找的东西:get-index-of-current-foreach-iteration 从该链接引用的答案: Foreach用于迭代实现IEnumerable的集合。 它通过调用集合上的GetEnumerator来完成