LINQ vs Loops like for, for each and while

Hi,

Can you please suggest me when to use LINQ, when to use loops like for, for each and while? What are the advantages of using LINQ over loops? Can LINQ increase speed of execution when compared to loops?

I’ve read somewhere that LINQ is faster than loops and vice-versa somewhere else. I’m not able to decide when to use LINQ and when not to use LINQ.

For example, is it a good idea to use LINQ in the following example or should I go with loops?

Thank you in advance

@ppr @ermanoj3101 @kumar.varun2 @Shubham_Varshney

@Surya_Narayana_Korivipadu
Just a few thoughts on this topic (preview of an howTo topic that is currently in preperation)

Task: update data columns
in general we should start with for each (sometimes also for each row) activity. We can combine it with some linq in case we want to filter in advance to resuce the amount of rows.

If for any reasons we want to do it with an invoke code activity we should mirror the for each activity instead of writing LINQ complexes. But for sure we can combine also both

Task Retrieval:
Filter DataTable is working fine, but we have less control on the input column (e.g. trim the input column, datatype conversions). So we can benefit from LINQ. But also the DataTable.Select method is very powerfull and very helpfully on constructing search expressions on the fly as we can formulate it on string base.

A common technique is to construct the datarow ItemArray within the LINQ and add it to a prepared empty datatable. Here we can profit as we can do retrieval and datarow structure construction in one go. (e.g. data joining and extracting information from left and right data column).

But in contrast e.g. the join datatable activity allows us to formulate join types / scenarios which are harder to express with LINQ. So again a combination can be helpfully: Join with join datatable activity, extract from join result with LINQ.

There are also other options making an update obsolete e.g. working with datacolumn expressions.

Decission Factors:

  • ensure the team mebers have a certain level of LINQ knowledge

  • only use LINQ in way that every team member can handle / maintain it

    • if needed train the members and relevel common the LINQ skills
  • avoid BlackBoxes:

    • a LINQ statement is Pass or Fail but if its failling, then we also need to find out what is going wrong.
    • Apply thechniques that will support you on issue analysis
    • Combine essential approaches with LINQ (e.g. Grouping data with LINQ, processing the group members with essential methods

About others e.g. Performance we would suggest to deal with facts instead of with opinions. The for each activity is not mandatory slowing down the processing. It depends on what is done / modelled / to compute

Let try to derive factors which lets help for decission:

  • Options for grouping data
    • LINQ, Dicitonary Approach, Filtering, cusstom activities e.g. from Marketplace
  • skill level from team (essential groupBy knowledge available?)
  • groupby member processing:
    • which processings have the risk to fail (e.g. invalid values / formats)
    • how to handle / analyse failings?
  • Datavolume
    … find your own factors as well

Once the criteria are applied and the different factors are known then it can be turned into a formal decission process (e.g. assigning points and count the points)

5 Likes

Hi @ppr

Thank you very much for detailed explanation.