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 a howTo topic that is currently in preparation)

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

If for any reason 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 over 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 powerful and very helpful in constructing search expressions on the fly as we can formulate it on a string base.

A common technique is to construct the data row ItemArray within the LINQ and add it to a prepared empty data table. Here we can profit as we can do retrieval and data row structure construction in one go. (e.g. data joining and extracting information from the 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 helpful: Join with join datatable activity, extract from join result with LINQ.

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

Decision Factors:

  • Ensure the team members have a certain level of LINQ knowledge

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

    • if needed train the members and re-level common the LINQ skills
  • avoid BlackBoxes:

    • a LINQ statement has a Pass or Fail result but if it is failing, then we also need to find out what is going wrong.
    • Apply techniques that will support you on issue analysis, when composing the LINQ expression
    • 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 dealing with facts instead of with opinions. The for each activity is not mandatory making slow down the processing. It depends on what is done / modelled / to compute

Let’s try to derive factors that lets help for decision (e.g. a Grouping Data Case):

  • Options for grouping data
    • LINQ, Dicitonary Approach, Filtering, custom activities e.g. from Marketplace
  • skill level from the team (essential groupBy knowledge available?)
  • groupby-member processing:
    • which processings have the risk to fail (e.g. invalid values/formats)
    • how to handle/analyze failings?
  • Data volume
    … 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 decision process (e.g. assigning points and counting the points)

Cross-reference:

8 Likes

Hi @ppr

Thank you very much for detailed explanation.