Recollect the basics of Date Format, String Manipulation and LINQ

Hello Everyone,

I would like to present this post who are looking for the expressions related to the Date Format, String manipulation and LINQ

Basic of Date formatting

In this below topic, you will learn about Date Formats

Convert datetime to week of year

System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(System.DateTime.Now, System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)

Last date previous month

New DateTime(Now.Year,Now.Month,1).AddDays(-1).ToString("dd/MM/yyyy")

First date previous month

New DateTime(Now.Year,Now.AddMonths(-1).Month,1).ToString(“dd.MM.yyyy”)

Current Month Last Date

(New DateTime(Today.AddMonths(1).Year,Today.AddMonths(1).Month,1)).AddDays(-1).toString("MM/dd/yyyy")

To Take First Date in a Month

Date.Now.AddDays(-(date.Now.Day-1)).Date.ToString(“dd/MM/yyyy”)

Getting Month for given culture

Assign activity

LHS → Create an variable (Country)
RHS → “Ro”

DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture(Country)).ToString

String Format

DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"),"dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("dd/MM/yyyy")

Condition for processing between two different time

New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second) > New DateTime(Now.Year, Now.Month, Now.Day, 07, 59, 0) And New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second) < New DateTime(Now.Year, Now.Month, Now.Day, 08, 59, 0) And Now.DayOfWeek.ToString <>"Saturday" And Now.DayOfWeek.ToString <>"Sunday"

Get the Last week Sunday

Today.AddDays(8 - Weekday(Today)-7)

Here is the workflow
Date Conditions.zip (55.9 KB)

Basics of string manipulation

In this below topic, you will learn about string manipulation,

  • Split
  • Substring
  • Insert
  • Replace
  • Remove
  • Trim

Example for Split method

Assign activity

LHS → Create an variable (Split_val)
RHS → “Hello! Welcome to uipath community!”

Log Message → "Splitting the value from the string - "+ Split(Split_val,"!")(1)

Variable Type → String

Output : Splitting the value from the string - Welcome to uipath community

Example for Substring method

Assign activity

LHS → Create an variable (Substring_val)

RHS → “Hello! Welcome to uipath community!”

Log Message → "Substring of the value - "+ SubString_val.Substring(7,17)

Variable Type → String

Output : Substring of the value - Welcome to Uipath

Example for Insert method

Assign activity

LHS → Create an variable (Insert_val)
RHS → “Hello! to Uipath community!”

Log Message → "Inserting the value from the string - "+ Insert_val.Insert(6," Welcome")

Variable Type → String

Output : Inserting the value from the string - Hello! Welcome to uipath community!

Example for Replace method

Assign activity

LHS → Create an variable (Replace_val)

RHS → “Hello! Welcome to Uipath community!”

Log Message → "Replacing the value from the string -"+ Replace_val.Replace("o","@")

Variable Type → String

Output : Replacing the value from the string - Hell@! Welc@me t@ uipath c@mmunity!

Example for Remove method

Assign activity

LHS → Create an variable (Remove_val)
RHS → “Hello! Welcome to uipath community!”

Log Message → "Removing the value from the string -"+ Remove_val.Remove(6)

Variable Type → String

Output : Removing the value from the string - Hello!

Example for Trim method

Trim → It will trim both the Whitespace at the Beginning and the End of the string
TrimStart → It will trim the Whitespace at the Beginning of the string
TrimEnd → It will trim the Whitespace at the End of the string

Assign activity

LHS → Create an variable (Trim_val)

RHS → “Hello! Welcome to Uipath community!”

Log Message → "Trimming the value - "+ Trim_val.Trim

Variable Type → String

Output : Trimming the value - Hello! Welcome to Uipath community!

Here is the workflow
StringManipulation.zip (55.4 KB)

Basics of LINQ expression

In this below topic, you will learn about LINQ

Example for working with Array Using LINQ

Query

Input

Explanation

Using a single LINQ query you can process data in various data sources.
You may change your data set without changing the LINQ query to process the data.

LINQ Query

(From Val In Str_ArrVal where Val.ToLower.Equals(StrValue) Select Val).ToArray

Example for working with Data Table Using LINQ

Query

Input

image

Explanation

We can use the different querying capabilities of the linq for performing different operations

LINQ Query

BuildDT.AsEnumerable.Where(Function(Val) Convert.ToInt32(Val.Item("Age").ToString)>20 And Convert.ToInt32(Val.Item("Age").ToString)<30).CopyToDataTable

Example for working with Group By Using LINQ

Query

Input

image

Explanation

Group By performs a deferred execution which means the query is only executed when the records are processed using a for each loop

LINQ Query

(From G In BuildDT.AsEnumerable
Group By O = New With {Key.K = G.Item("RollNo"), Key.U = G.Item("RollNo")} 
Into Group Select Group(0)).ToArray().copytodatatable()

Example for working with Joins Using LINQ

Query

Input

image

image

Explanation

It combines different source elements into one and also creates the relationship between them.
And also need to clone the Data Table

LINQ Query

(From G In BuildDT1 Join O In BuildDT2 On G(0).tostring Equals O(0).tostring
Select OutputDT.Clone.LoadDataRow(New Object(){G(0).tostring,G(1).tostring,O(1).tostring},False)).copytodatatable

Example for working with List Using LINQ

Query

Input

Uploading: image.png…

Explanation

LINQ to Lists/collection means writing the LINQ queries on list or collection.
By using LINQ queries on the collection or list, we can filter or sort or remove the duplicates elements.

LINQ Query

(From Val In Str_ListVal where Val.ToLower.Equals("sample1") Select Val).ToList

Example for working with Sort Using LINQ

Query

Input

image

Explanation

LINQ Reverse method is used to reverse the data stored in a data source.
sort the sequence (collection) based on particular property in ascending or descending order.

LINQ Query

Sort Reverse
(BuildDT.AsEnumerable.Reverse).copytodatatable

Sort OrderBy
BuildDT.AsEnumerable.OrderBy(Function(Val) Val("RollNo")).ThenBy(Function(Val) Val("Name")).CopyToDataTable

Example for working with To Lookup Using LINQ

Query

Input

image

Explanation

In this example we are looking the RollNo. In a Lookup we can have multiple values with a single key

LINQ Query

BuildDT.AsEnumerable.ToLookup(Function(Val) Val("RollNo"))

Reference

Here is the workflow

LINQexpression.zip (57.0 KB)

Hope this will be helpful :slight_smile:

Questions

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

Feedback

Click image button to share your thoughts

@loginerror

Regards,
Gokul

21 Likes

Great post.

I think below expression needs to be corrected to
New DateTime(Now.AddMonths(-1).Year,Now.AddMonths(-1).Month,1).ToString("dd/MM/yyyy")

Thanks

1 Like

Thanks for sharing your thoughts @poorna_nayak07

This expression is to get the first date of Pervious month.

Regards,

I have updated my post

Thanks for your responses @poorna_nayak07

Great one @Gokul001. Keep posting
Thanks

1 Like

Thanks @Venkatesh_R1

1 Like