Linq Query to update only specific columns based on condition

Hi Team ,

I have a Query regarding Linq. My problem statement is I have do a left join for two tables.
My dt1 - student No , Student Name , Student Average score, … extra columns
My Dt2 - Student roll no , Student Score , Student City , Student state … Extra columns

My output dt - Student No(dt1) , Student Name(dt1) ,Student Avg score(dt1) , Avg score (dt2), student city , student state

Its kind of like a vlookup where student no. are matching in both the sheets then add relevant dt2 columns else write as NA.

I tried this linq but its showing as ‘)’ expected compilation error

(From row1 In dt1.AsEnumerable()
Join row2 In dt2.AsEnumerable()
On row1.Field(Of String)(“Student no”) Equals row2.Field(Of String)(“student no”) Into joinedRows = Group
From row2 In joinedRows.DefaultIfEmpty()
Select outputdt.Rows.Add(row1(“student no”), row1(“student name”), row1(“Student score”), row1(“student age”),
row1(“student branch”),
If(row2 Is Nothing OrElse row2.IsNull(“student score”), “NA”, row2.Field(Of String)(“student score”)),
If(row2 Is Nothing OrElse row2.IsNull(“student state”), “NA”, row2.Field(Of String)(“student state”)),
If(row2 Is Nothing OrElse row2.IsNull(“student city”), “NA”, row2.Field(Of String)(“student city”)),“OK”
)).CopyToDataTable()

Pls check if there is any syntax error or any simpler version , I tried to first copy all the required columns from dt1 but I’m not able to just update remaining columns based on condition.

@Ana_Patricia

if possible can you provide any sample input , so That we may help you

or

please explain more about your requirement

Sure ,
Example Values
dt1

Student No Student Name Student Age Student Branch Student Score ………
1 Vinay 12 Branch A 80 ……
2 Madhav 10 Branch B 65 …….

dt2

ID Score State City Grade
1 80 UP ik B
5 90 MP ts A

Expected Output

Student No Student Name Student Age Student Branch Student Score Score State
1 Vinay 12 Branch A 80 80 UP
2 Madhav 10 Branch B 65 NA NA

Query I used

(From row1 In dt1.AsEnumerable()
Join row2 In dt2.AsEnumerable()
On row1.Field(Of String)(“Student no”) Equals row2.Field(Of String)(“student no”) Into joinedRows = Group
From row2 In joinedRows.DefaultIfEmpty()
Select outputdt.Rows.Add(row1(“student no”), row1(“student name”), row1(“Student score”), row1(“student age”),
row1(“student branch”),
If(row2 Is Nothing OrElse row2.IsNull(“student score”), “NA”, row2.Field(Of String)(“student score”)),
If(row2 Is Nothing OrElse row2.IsNull(“student state”), “NA”, row2.Field(Of String)(“student state”)),
If(row2 Is Nothing OrElse row2.IsNull(“student city”), “NA”, row2.Field(Of String)(“student city”)),“OK”
)).CopyToDataTable()

we keep in mind that Data Table reconstruction strategy for a Left join is tried

As a starter sample have a look here:

(From d1 In dt1.AsEnumerable()
Group Join d2 In dt2.AsEnumerable()
On d1("Student no").toString.Trim Equals d2("student no").toString.Trim 
Into joinedRows = Group
From jrow In joinedRows.DefaultIfEmpty()
Let arrSet1 = New String() {"studen no","student name","Student Core", "student age", "student branch"}
Let arrSet2 = New String() {"student score","student state","student city"}
Let ra1 = arrSet1.Select(Function (e1) d1(e1))
Let raEmpty = arrSet2.Select(Function (ee) "NA").Cast(Of Object)
Let ra2 = If(isNothing(jrow), raEmpty, arrSet2.Select(Function (e2) jrow(e2)))
Let ra = ra1.Concat(ra2).Cast(Of Object).toArray  
Select r = outputdt.Rows.Add(ra)).CopyToDataTable
1 Like

Hi @Ana_Patricia

Try this:

(From row1 In dt1.AsEnumerable()
Join row2 In dt2.AsEnumerable()
On row1.Field(Of String)("Student No") Equals row2.Field(Of String)("ID") Into joinedRows = Group
From row2 In joinedRows.DefaultIfEmpty()
Select outputdt.Rows.Add(row1("Student No"), row1("Student Name"), row1("Student Age"), row1("Student Branch"), row1("Student Score"),
If(row2 Is Nothing OrElse row2.IsNull("Score"), "NA", row2.Field(Of String)("Score")),
If(row2 Is Nothing OrElse row2.IsNull("State"), "NA", row2.Field(Of String)("State"))
)).CopyToDataTable()

Hope it helps!!

may we ask you to share with us

  • the sucessfully compilation screenshot
    details (Legacy?, Windows?, UiPath.System.Activities Version)

It would help us for the cross checks. Thanks

Hey , Thank you so much !!! This worked. But still I didnt get what was the issue with the Linq syntax I was using.

But Im trying another linq for another scenario to update the column in the same sheet based on some conditions:
Example : I need to update the status column based on some Input column being not zero & need to match months of two columns . Pls check this Linq Query & let me know where is the mistake:

Im getting error as ‘Range variable name can be inferred only from a simple or qualified name with no arguments

(From row In outputdt.AsEnumerable()
Let inputAmount = Convert.ToDecimal(row(“Input”).ToString())
Let endDateMonth = DateTime.ParseExact(row(“End Date”).ToString(), “MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture).Month
Let month = DateTime.ParseExact(row(“last mnth”).ToString(), “MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture).Month
Let Bonus = Convert.ToDecimal(row(“BONUS”).ToString())
Let remarks = If(inputAmount <> 0 AndAlso endDateMonth = month,
If(inputAmount = Bonus, “found”, “not found”),
“no input”)
Select row.Field(Of String)(“Emp code”),
row.Field(Of String)(“Emp name”),
row.Field(Of DateTime)(“first Mnth”),
row.Field(Of DateTime)(“last mnth”),
row.Field(Of Decimal)(“Bonus Amount”),
row.Field(Of Decimal)(“Input”),
row.Field(Of DateTime)(“Start Date”),
row.Field(Of DateTime)(“End Date”),
remarks).CopyToDataTable()

@Ana_Patricia
we are wondering on the solution flag

As a Left Join was targeted the clause syntax defines the group keyword

then we will use the

For inner Join (without Group keyword) we would expect a failing compilation

for this new use case we would recommend to open a new topic and get scoped 1 Topic = 1 case

Thank you , Solution flag because the Query worked for the requirement on Group join between those two tables & combining the selected columns to a new array ra

thats why we asked for feedback:

we are strongly interrested on the feedback:
Is an Assign acitvity compiling / LINQ accepted when

Join row2 In dt2.AsEnumerable()
On row1.Field(Of String)("Student No") Equals row2.Field(Of String)("ID") Into joinedRows = Group

A join with the construct Into joinedRows = Group is used BUT without Group Keyword before Join

As in the past and also in RnD crosschecks this is a compiler complain expectation

Framework - Legacy
System activities Version - 21.4.1


Yes I used it in Assign & its accepted the Group Keyword. I didnt get any compilation issues - I just changed the datatype parsing as
on if(Double.TryParse(row.Field(of String)(“Student No”),Nothing) , convert.ToDouble(row1.Field(of String)(“Student No”)),Double NAN) _ equals Convert.ToDouble(row2(“ID”).ToString)

Also as suggested I have created another topic for my other Linq Query

Yes, But we asked for the construct of missing Group

Group Join … Into joinedRows = Group
OK, Expected

not Flagged as Solution

Join … Into joinedRows = Group
(here without prepending group)
Requested more times for Feedback, (Screenshot of compiling Join without Group and used into construct)
Not expexcing that it will compile,
not found in Docu with this definition
not compiling in out cross tests

BUT flagged as solution

And that why we want to know this detail and if there is a change / missmatch with the docu

Mistake from my end , I liked the solution provided by you with ‘Group’ & thought the same was marked as solution. My Apologies …!! I made the other one as solution by mistake.
Corrected it now.!! Thank you so much for all the detailed explanations

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.