Lambda expression to Iterate through a collection and edit C#

Gokul

How do I do this using a Lambda expression?

foreach (var l in urllist)

{
  l.Url = l.Url + "?id=" + client.id+ "&active=" + client.Active;
}
Tim Schmelter

Don't use Linq to modify collections, use loops instead:

So this is the best approach (have a look at the UriBuilder Class):

foreach (var l in urllist)
{
    l.Url = string.Format("{0}?id={1}&active={2}", l.Url, client.id, client.Active);
}

You have to create a new collection otherwise what is less efficient than modifying the original collection:

urllist = urllist
    .Select(l => {l.Url = string.Format("{0}?id={1}&active={2}", l.Url, client.id, client.Active; return l;})
    .ToList();

If urllist is a List<T> you could also use the pre-linq .NET 2 method ForEach:

urllist.ForEach(l => l.Url = string.Format("{0}?id={1}&active={2}", l.Url, client.id, client.Active));

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

MongodbCursor , how to iterate through Huge collection?

분류에서Dev

Iterate through json object in c#

분류에서Dev

Lambda Expression in c++ std::copy_if

분류에서Dev

Is there a java lambda expression equivalent of the C# linq let?

분류에서Dev

How to iterate through XML in Powershell?

분류에서Dev

Recursively iterate through files in a directory

분류에서Dev

How to iterate through nested classes?

분류에서Dev

Iterate through JSON and pass it to tokeninput

분류에서Dev

Iterate through ListView in custom Adapter

분류에서Dev

Iterate through points in an arbitrary shape

분류에서Dev

Iterating through a firebase collection

분류에서Dev

Function taking lambda expression

분류에서Dev

std::pair in lambda expression

분류에서Dev

Lambda expression select and add

분류에서Dev

AddHandler Lambda Expression

분류에서Dev

Generic with lambda expression

분류에서Dev

Confused about about how to iterate this collection

분류에서Dev

Generate immutable collection in one expression

분류에서Dev

Looping through an Eloquent collection and redirecting

분류에서Dev

iterate through an enum type initialized with a string name?

분류에서Dev

how to iterate through xml child elements

분류에서Dev

How to iterate through 2 DataSources in SoapUI Pro

분류에서Dev

python iterate through binary file without lines

분류에서Dev

iterate through mongodb entry javascript node

분류에서Dev

How can I iterate through a function with for loop?

분류에서Dev

How to iterate through all textblocks in a stackpanel

분류에서Dev

Iterate Through Array in Bash; Exit if No Matches are Found

분류에서Dev

c++: priority_queue: lambda-expression in template-argument

분류에서Dev

C# show data of ExpandoObject - Cannot convert lambda expression to type '…' because it is not a delegate

Related 관련 기사

뜨겁다태그

보관