[HowTo] LINQ - First Start # Concat | Append | Prepend Operator

This HowTo gives an introductory overview of the conjunction operators: Concat, Append, Prepend

Introduction

The conjunction operators are used to add the item(s) to a particular set of items.

Concat, Append, Prepend Operator

The Concat Operator combines a set of items with another set of items
The Append Operator adds an item to a set of items on the end
The Prepend Operator adds an item to a set of items on the begin

Overview

I – Input P – Processing O – Output
{"A","B"} / {"C","D"} combine both sets of items into single one {"A","B","C","D"}
{"A","B"} / "#E" add an item to a set of items at the end {"A","B","#E"}
{"A","B"} / "#S" add an item to a set of items at the begin {"#S",A","B"}

Implementation

Method Syntax

arrValues1 | String( ) - a String Array =  {"A","B"}
arrValues2 | String( ) - a String Array =  {"C","D"}

arrResult = arrValues1.Concat(arrValues1).ToArray()
arrResult = arrValues1.Append("#E").ToArray()
arrResult = arrValues1.Prepend("#S").ToArray()
Visuals

grafik

Reminder:

  • Using these LINQ Operators the returned set of items can have a count of 0,1 or more.
  • The returned LINQ generic baselined datatype will be transformed into a targeted datatype - in this case a String Array with the usage of ToArray() at the statement end

Query Syntax

The Concat, Append, and Prepend operator is not available as a building block in the Query Syntax. But a statement written within the Query Syntax can be combined with an appended Concat, Append, or Prepend operator as method syntax. As a result, the strength of both can be utilized.

arrStatus = {"New","WIP","Done","Failed"}
arrStatusOrdered = 
(From x in arrUnorderedStatus
Let i = Array.IndexOf(arrStatus,x)
Select s = (i + 1).ToString().LeftTrim(2,"0"c) & "-" & x).Prepend("00-Undefined").ToArray()

Notes about the Append and Prepend Operator

  • The Append, Prepend Operator applies to:
    • .NET Core 1.0 and above,
    • .NET Framework 4.7.1 and above
    • .NET Standard 1.6 and above
  • The Append and Prepend operators can be used if the appropriate level of the target framework is available on the executing machine
  • The Append / Prepend operator does not change the original collection but returns a created copy with the appended/prepended element

Samples

Datatable

(From i In Enumerable.Range(0,dtMaster.Rows.Count)
Let ra = dtMaster.Rows(i).ItemArray.Prepend(i).ToArray()
Select dt1Indexed.Rows.Add(ra)).CopyToDataTable
(From d1 In dt1.AsEnumerable()
Join d2 In dt2.AsEnumerable()
On d1(0).ToString().Trim() Equals  d2(0).ToString().Trim()
Let ra = d1.ItemArray.Concat({d2(3),d2(5)}).toArray()
Select dtResult.Rows.Add(ra)).CopyToDataTable()

Collections

List1.Concat(arrNewNames).ToList()

References

Documentation

Recommendations

  • Training courses from Academy
  • Debugging course from Academy (Highly recommended)

Questions

For questions on your case open a new topic and get individual support

3 Likes