2

If I want to iterate through a collection, and call a function on each element in the collection, I could go with :

foreach (var obj in objColl)
{
    MyFunction(obj);
}

Should I want to do this with linq, I can use either of those :

//#1
var unused = (from var obj in objColl select MyFunction(obj)).ToList();

//#2
var unused = objColl.Select(obj => MyFunction(obj)).ToList();

I know this works, but it doesn't seem right. Of course, my actual cases are more complex queries that that, but it comes down to this since I could build my IQueryable with Linq and iterate through it and call the function.

Edit: Here is one example of what I did. (Item# are things I can't disclose)

var dummyValue = (from
                  Item7 in dal.GetAgencyConvertions().Where(age => age.SourceName == "Item1" && age.TargetName == "Item2")
                  join Item6 in dal.GetAgencyConvertions().Where(age => age.SourceName == "Item2" && age.TargetName == "Item3") on Item6.TargetValue equals Item7.SourceValue
                  join agency in dal.GetAgencies() on Item7.SourceValue equals agency.Agency
                  orderby Item7.TargetValue
                  select vl.ValueListItems.Add(agency.ID, Item7.TargetValue)).ToList();

3 Answers 3

5

Go with the simple foreach, as you are clearly wanting to perform an action on (and/or using) the objects in your collection as opposed to wishing to project/filter/group/etc. the items in the sequence. LINQ is about the latter set of operations.

Edit: In the case of your update, I would simply create a query, and then iterate over the query in the foreach to perform the action.

var query = from Item7 in dal.GetAgencyConvertions().Where(age => age.SourceName == "Item1" && age.TargetName == "Item2") 
            join Item6 in dal.GetAgencyConvertions().Where(age => age.SourceName == "Item2" && age.TargetName == "Item3") on Item6.TargetValue equals Item7.SourceValue  
            join agency in dal.GetAgencies() on Item7.SourceValue equals agency.Agency 
            orderby Item7.TargetValue 
            select new { ID = agency.ID, Value = Item7.TargetValue };

foreach (var item in query)
    vl.ValueListItems.Add(item.ID, item.Value);

To be frank, you have the same loop happening in your code, you merely mask it by using the ToList() extension method. As a byproduct, you are creating a list of values that you have no intention of using, while somewhat obfuscating the true intention of the code, all to save maybe a few characters.

1
  • I'll go with this, even though the result in CIL would most likely be the same, just to keep the code clear. Thanks.
    – Tipx
    Dec 13, 2010 at 20:35
1

Typically, a query shouldn't have any side effects (i.e. it shouldn't modify the state of the data or other data in your application) which raises the question, does MyFunction modify the state of your application? If it does, then you should use a foreach loop.

1
  • Well, one case I have is to create a value list. If your not familiar with it, it's an Infragistics kind of custom dictionary. I'll update my question to have this example as well.
    – Tipx
    Dec 13, 2010 at 19:32
1

How about an Each() extension method?

public static void Each<T>(this IEnumerable<T> target, Action<T> action)
{
    if (target == null) return;
    foreach (T obj in target)
        action(obj);
}
1
  • Not a terrible answer, but there are, I think, some solid opinions on why not to do this. :)
    – Dan J
    Dec 13, 2010 at 19:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.