0

I am trying to convert this code to linq:

foreach (var printer in printers)
{
    if (printer.Installed)
        installedPrinters.Add(printer);
}

I am new to Linq and would appreciate pointers on how it works when iterating through a collection.

5 Answers 5

3
printers.Where(printer => printer.Installed)
  .ToList()
  .ForEach(printer => installedPrinters.Add(printer));

Note the need to call ToList() before ForEach (see Lambda Expression using Foreach Clause).

Also note that while this works, your original code is probably easier to read... LINQ is cool but don't feel obligated to use it for everything :)

Sign up to request clarification or add additional context in comments.

6 Comments

I'd also mention that one of the principal developers of c# compiler team (Eric Lippert) doesn't particularly like the LINQ .ForEach() (nor do I). The first reason is that doing so violates the functional programming principles that all the other sequence operators are based upon. Clearly the sole purpose of a call to this method is to cause side effects. The purpose of an expression is to compute a value, not to cause a side effect. The purpose of a statement is to cause a side effect.
More simply, printer => installedPrinters.Add(printer) could be installedPrinters.Add.
@Ripple and how do you suppose your alternative would know which printer in the foreach to install?
@ErikPhilips ForEach has a signiature of Action<Printer> which void Add(Printer printer) fulfills. It just passes the argument in the same way the lambada does. (I agree with your fist comment too btw, he should not be using ForEach)
@ErikPhilips Each printer will be passed by ForEach as @Scott commented. In addition, I assume installedPrinters.Add returns void. Sorry for my insufficient words.
|
3

If you are just trying to create a new list, you could always just do:

var installedPrinters = printers.Where(p => p.Installed).ToList();

If you are adding to a list that may already have items in it, then you could try:

installedPrinters.AddRange(printers.Where(p => p.Installed));

Assuming your installedPrinters is actually a collection that supports AddRange such as List.

Comments

1

So first use a Where to filter the Installed==true, then run over them with ForEach:

printers.Where(p => p.Installed).ForEach(p => installedPrinters.Add(p));

1 Comment

Note that Robert's answer is more accurate as it includes the ToList
0
foreach (var printer in printers.Where (p => p.Installed) { installedPrinters.Add(printer); }

Comments

0

Try this

printer.Where(x => x.Installed).ToList()
      .ForEach(
          p=>
            {
              installedPrinters.Add(p)
            }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.