Linq query to Get Excel data based on Condition

Needed Linq Query for following Scenario, I have one excel with below data

Name Country Address Marks Status Diff_1 Diff_2
A IND IND 56 Pass TRUE FALSE
B USA USA 76 Pass TRUE TRUE
C USA USA 87 Pass FALSE FALSE
D IND IND 98 Pass FALSE TRUE
E USA USA 0 Fail TRUE TRUE
F IND IND 9 Fail FALSE TRUE
G USA USA 45 Fail FALSE FALSE
H IND IND 67 Pass TRUE TRUE
I IND IND 87 Pass FALSE FALSE

First it will check difference between 2 column Diff_1 & Diff_2, if True and False = False then i want to select particular column data

Expected Output:

Name Marks Status
A 56 Pass
C 87 Pass
D 98 Pass
F 9 Fail
G 45 Fail
I 87 Pass

@Yoichi @Anil_G Could you please look into this

Hi @Random_Bot

=> Build Data Table
image
Output-> dtOutput

=> Read Range Workbook
image
Output-> dtInput

=> Use below syntax in Assign:
dtOutput = (From row In dtInput.AsEnumerable() Let diff1 = row.Field(Of Boolean)("Diff_1") Let diff2 = row.Field(Of Boolean)("Diff_2") Where (diff1 = True AndAlso diff2 = False) OrElse (diff1 = False AndAlso diff2 = False) OrElse (diff1 = False AndAlso diff2 = True) Select dtOutput.Rows.Add(row.Field(Of String)("Name"), row.Field(Of Double)("Marks"), row.Field(Of String)("Status"))).CopyToDataTable()

=> Write Range Workbook dtOutput.
image


Sequence3.xaml (9.9 KB)

Hope it helps!!

3 Likes

Hey @Random_Bot

Use below mentioned LinQ:

dt_Input = dt_Input.AsEnumerable.Where(Function(x) Not (CBool(x("Diff_1").ToString) And CBool(x("Diff_2").ToString))).AsDataView.ToTable(False,{"Name","Marks","Status"})

Screenshot for your reference:

Regards,
Ajay Mishra

2 Likes

This looks good to me; I double-checked it. :slight_smile:

1 Like

@Ajay_Mishra

this linq looks simple,short and understandable.

I think this should also work; it saves 2 lines in the assignment.

image

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