Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linq to run a method on a collection of objects?

Tags:

c#

linq

This is a long shot, I know...

Let's say I have a collection

List<MyClass> objects;

and I want to run the same method on every object in the collection, with or without a return value. Before Linq I would have said:

List<ReturnType> results = new List<ReturnType>();
List<int> FormulaResults = new List<int>();
foreach (MyClass obj in objects) {
  results.Add(obj.MyMethod());
  FormulaResults.Add(ApplyFormula(obj));
}

I would love to be able to do something like this:

List<ReturnType> results = new List<ReturnType>();
results.AddRange(objects.Execute(obj => obj.MyMethod())); 
// obviously .Execute() above is fictitious
List<int> FormulaResults = new List<int>();
FormulaResults.AddRange(objects.Execute(obj => ApplyFormula(obj)));

I haven't found anything that will do this. Is there such a thing?

If there's nothing generic like I've posited above, at least maybe there's a way of doing it for the purposes I'm working on now: I have a collection of one object that has a wrapper class:

class WrapperClass {
  private WrappedClass wrapped;
  public WrapperClass(WrappedClass wc) {
    this.wrapped = wc;
  }
}

My code has a collection List<WrappedClass> objects and I want to convert that to a List<WrapperClass>. Is there some clever Linq way of doing this, without doing the tedious

List<WrapperClass> result = new List<WrapperClass>();
foreach (WrappedClass obj in objects)
  results.Add(new WrapperClass(obj));

Thanks...

like image 641
Shaul Behr Avatar asked Jun 23 '09 12:06

Shaul Behr


People also ask

What collections can LINQ be used with?

You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey,TValue>. The collection may be user-defined or may be returned by a . NET API.

What are the uses of LINQ to objects?

In a nutshell, LINQ to Objects provides the developer with the means to conduct queries against an in-memory collection of objects. The techniques used to query against such collections of objects are similar to but simpler than the approaches used to conduct queries against a relational database using SQL statements.

Can LINQ query work with array?

Yes it supports General Arrays, Generic Lists, XML, Databases and even flat files. The beauty of LINQ is uniformity.

Can we use LINQ on ArrayList in C#?

LINQ with ArrayList You don't need to specify array size unlike traditional array and the size grows as you will add more element into it. However, it is slower than Array but it is more useful when you work with collection. Here, in this example, we will learn how to process data in ArrayList using LINQ C#.


2 Answers

Would:

results.AddRange(objects.Select(obj => ApplyFormula(obj)));

do?

or (simpler)

var results = objects.Select(obj => ApplyFormula(obj)).ToList();
like image 64
Marc Gravell Avatar answered Oct 07 '22 21:10

Marc Gravell


I think that the Select() extension method can do what you're looking for:

objects.Select( obj => obj.MyMethod() ).ToList(); // produces List<Result> 

objects.Select( obj => ApplyFormula(obj) ).ToList(); // produces List<int>

Same thing for the last case:

objects.Select( obj => new WrapperClass( obj ) ).ToList();

If you have a void method which you want to call, here's a trick you can use with IEnumerable, which doesn't have a ForEach() extension, to create a similar behavior without a lot of effort.

objects.Select( obj => { obj.SomeVoidMethod(); false; } ).Count();

The Select() will produce a sequence of [false] values after invoking SomeVoidMethod() on each [obj] in the objects sequence. Since Select() uses deferred execution, we call the Count() extension to force each element in the sequence to be evaluated. It works quite well when you want something like a ForEach() behavior.

like image 35
LBushkin Avatar answered Oct 07 '22 20:10

LBushkin