I’ve a data table called “QueueDefinitionsDT” like this:
QueueDefinitionsDT
Name
Id
OEQ
3
HPA
74
Queu3
83
BD
130
VQ
95
Second data table called “QueueItemsDT” like this:
QueueItemsDT
QueueDefinitionId
Status
Key
ProcessingExceptionType
StartProcessing
EndProcessing
Id
130
Successful
cf406435-29b9-42db-b0ce-90fea514577d
08-02-2021 11:35
08-02-2021 11:50
10090
130
Successful
e6e15baf-5fdd-4540-b271-11caa6313145
08-02-2021 14:09
08-02-2021 14:21
10094
130
Successful
7d66b736-ffaa-49f5-a66c-1c059cca81a2
BusinessException
08-02-2021 14:57
08-02-2021 15:06
10095
74
Successful
d0816eb5-379c-45a2-bf1a-7d8b7da9c2d9
08-03-2021 11:00
08-03-2021 11:08
10160
3
Failed
64949faa-11e7-403c-aabe-ab5705c7a072
ApplicationException
08-03-2021 11:22
08-03-2021 11:28
10161
3
Successful
fe771c04-46bd-4589-994e-867b9d3cc65b
08-03-2021 13:52
08-03-2021 14:00
10173
The column “Id” in “QueueDefinitionsDT” and the column “QueueDefinitionId” in “QueueItemsDT” refer to same data. If Id is 3 and the name is “OEQ” in QueueDefinitionsDT, then it means that QueueDefinitionId - 3 in QueueItemsDT refers to the name “OEQ”.
So I need a resulting data table called “FinalQueueDT” like this:
FinalQueueDT
QueueDefinitionId
Status
Key
ProcessingExceptionType
StartProcessing
EndProcessing
Id
QueueName
130
Successful
cf406435-29b9-42db-b0ce-90fea514577d
08-02-2021 11:35
08-02-2021 11:50
10090
BD
130
Successful
e6e15baf-5fdd-4540-b271-11caa6313145
08-02-2021 14:09
08-02-2021 14:21
10094
BD
130
Failed
7d66b736-ffaa-49f5-a66c-1c059cca81a2
BusinessException
08-02-2021 14:57
08-02-2021 15:06
10095
BD
74
Successful
d0816eb5-379c-45a2-bf1a-7d8b7da9c2d9
08-03-2021 11:00
08-03-2021 11:08
10160
HPA
3
Successful
d0816eb5-379c-45a2-bf1a-7d8b7da9c2d9
08-03-2021 11:00
08-03-2021 11:08
10160
OEQ
3
Failed
64949faa-11e7-403c-aabe-ab5705c7a072
ApplicationException
08-03-2021 11:22
08-03-2021 11:28
10161
OEQ
Can someone please guide me how to achieve this without using loops? Because I feel that using loops will take too much to handle huge data.
Please note that QueueItemsDT may not contain all queue IDs, but QueueDefinitionsDT contains all queue names and IDs. Whichever queues are present in QueueItemsDT, I need to match a name to it based on QueueDefinitionsDT
FinalQueueDT= QueueItemsDT.Clone
Add DataColumn Activity - Add the QueueName DataColumn to the empty/cloned FinalQueueDT
Assign Activity:
Left side: FinalQueueDT
Right side:
(From d in QueueItemsDT.AsEnumerable
Let qn = QueueDefinitionsDT.Where(Function (f) f("Id").toString.Trim.Equals("d("QueueDefinitionId").toString.Trim)).Select(Function (x) x("Name").toString).DefaultIfEmpty("Undefined").First()
Let ra = d.ItemArray.Append(qn)
Select r= FinalQueueDT.Rows.Add(ra)).CopyToDataTable
The id column name in QueueItemsDT is “QueueDefinitionId” which matches with QueueDefinitionsDT - Id column. So I assume I should use d(“QueueDefinitionId”) in second line of your linq query. Am I correct?
But as per my understanding it should be f(“Id”) and then d(“QueueDefinitionId”), because f belongs to QueueDefinitionsDT and d belongs to QueueItemsDT. Can you please correct me if my understanding is wrong?
I assume your linq query is taking each Id from QueueDefinitionsDT and trying to match to QueueDefinitionId for every row in QueueItemsDT and then preparing Name column in FinalQueueDT. Am I correct? If not, can you please elaborate what this linq query does?
You are correct. It was just swapped. Can you test again?
Let qn = QueueDefinitionsDT.Where(Function (f) f("Id").toString.Trim.Equals("d("QueueDefinitionId").toString.Trim)).Select(Function (x) x("Name").toString).DefaultIfEmpty("Undefined").First()
Here we are searching in QueueDefinitionsDT for the Id from our QueueItemsDT row
In case of we do not find then we are returning a default value “Undefined” in our imp
Why would you copy the value from one table to another? This is two tables in a relational format, and you can use a SQL join to get the resulting data you want without actually copying the value. This is valuable, because if something changes in one table, you don’t have to worry about updating the other table - it’s relational. The point to relational data is you don’t have to copy values from one table to another.
SELECT * FROM QueueDefinitionsDT.*, QueueItemsDT.* FROM QueueItemsDT INNER JOIN QueueDefinitionsDT ON QueueDefinitionsDT.Id = QueueItemsDT.QueueDefinitionId
yes. I’m learning from tutorialsteacher.com as you said in your previous replies to my another posts.
Sorry to bother you once again. Data is not in DB. It’s directly in data table variables. I tried .AsEnumerable for both data tables and I got the following error:
Multiple Assign - Final Queue DT: Can not assign ‘(From d In QueueItemsDT.AsEnumerable
Let qn = QueueDefinitionsDT.AsEnumerable.Where(Function (f) f(“Id”).tostring.trim.equals(d(“QueueDefinitionId”).tostring.trim)).Select(Function (x) x(“Name”).tostring).DefaultIfEmpty(“Undefined”).First()
Let ra = d.ItemArray.Append(qn)
Select r= FinalQueueDT.Rows.Add(ra)).CopyToDataTable’ to ‘FinalQueueDT’.
I’m not able to understand what went wrong. Can you please help?
May we ask you to compare to your implementation thanks
OK, so things cannot be done in SQL directly
As an alternate find a inner join approach here:
(From d1 In QueueItemsDT.AsEnumerable
Join d2 In QueueDefinitionsDT.AsEnumerable
On d1("QueueDefinitionId").toString.Trim Equals d2("Id").toString.Trim
Let qn =d2("Name").tostring
Let ra = d1.ItemArray.Append(qn)
Select r= FinalQueueDT.Rows.Add(ra)).CopyToDataTable
Ensutre that from Copy & Paste all " are correctly taken and no “ or ” is used
Doesn’t matter. Inner Join will only give you matching rows. Outer Join will give you all rows from one table, plus matching rows from the other table.
“QueueItemsDT contains many rows of data where QueueDefinitionId can be a duplicate.”
This linq which prepares FinalQueueDT is valid syntactically. I cross checked and it looks correct. Also updated case properly in toString, Trim and Equals. Still I’m getting same error.
Here it is:
Multiple Assign - Final Queue DT: Can not assign ‘(From d In QueueItemsDT.AsEnumerable
Let qn = QueueDefinitionsDT.AsEnumerable.Where(Function (f) f(“Id”).toString.Trim.Equals(d(“QueueDefinitionId”).toString.Trim)).Select(Function (x) x(“Name”).toString).DefaultIfEmpty(“Undefined”).First()
Let ra = d.ItemArray.Append(qn)
Select r= FinalQueueDT.Rows.Add(ra)).CopyToDataTable’ to ‘FinalQueueDT’.
This time I used “FinalQueueDT” in both left hand side and right hand side of assign activity
use single assign
use statement as tested and provided (Post 14)
About ValidationMessage - if it is failing then create screenshot from hover containing the message
Test alternate Join Syntax