Pass variable to linq?

Now I use linq as below.

But I want change from text Voucher to variable in TypeVoucher (voucher,central)

Please guide me about it.

2 Likes

Hi

Remove that double quotes and mention just your variable name inside that bracket

Like this Equals(Variablename.ToString)

As yours is a array mention like this

…Equals(TypeVoucher(0).ToString)

Where 0 is the index position of term Voucher in your array

Or if you want to check the value of column Premium with this entire array then mention like this

OutputDT.AsEnumerable().Where(Function(x) TypeVoucher.Contains(x.Field(of String)(“Premium”).ToString.Trim)).CopyToDatatable()

Hope this would help you resolve this

Cheers @fairymemay

1 Like

Hey @fairymemay

I hope you want to check whether the string is equal to any of the array elements.

OutputDT.AsEnumerable.Where(Function(x) TypeVoucher.Contains(x("Premium").ToString)

Hope this helps

Thanks
#nK

original linq as below.

OutputDT.AsEnumerable.Where(Function(x) (x("Premium").ToString.Equals("Voucher") andAlso x("Type Special").ToString.Equals("Yes")) OrElse ((x("Premium").ToString.Equals("เงินคืนเข้าบัญชีเงินฝาก SCB") or x("Premium").ToString.Equals("เงินคืนเข้าบัตรเครดิต SCB")) AndAlso x("Type Special").ToString.Equals("Yes") AndAlso String.IsNullOrWhiteSpace(x("Account No").ToString))).ToArray

It I change follow you suggest as below.

OutputDT.AsEnumerable.Where(Function(x) TypeVoucher.Contains(x("Premium").ToString) andAlso x("Type Special").ToString.Equals("Yes")) OrElse ((x("Premium").ToString.Equals("เงินคืนเข้าบัญชีเงินฝาก SCB") or x("Premium").ToString.Equals("เงินคืนเข้าบัตรเครดิต SCB")) AndAlso x("Type Special").ToString.Equals("Yes") AndAlso String.IsNullOrWhiteSpace(x("Account No").ToString))).ToArray

Please guide me for solve it.

1 Like

Hey @fairymemay

Kindly check this now please…

It was due to a bracket mismatch.

OutputDT.AsEnumerable.Where(Function(x) _
	TypeVoucher.Contains(x("Premium").ToString) AndAlso
	x("Type Special").ToString.Equals("Yes") OrElse
	(x("Premium").ToString.Equals("เงินคืนเข้าบัญชีเงินฝาก SCB") Or x("Premium").ToString.Equals("เงินคืนเข้าบัตรเครดิต SCB")) AndAlso
	x("Type Special").ToString.Equals("Yes") AndAlso String.IsNullOrWhiteSpace(x("Account No").ToString)).ToArray

Thanks
#nK

3 Likes

Perfect

You are almost done

Usually this error means like we just missed some symbols like ) or , along the expression

I will align it in a way to identify where we missed

————————————————————————
This is the corrected expression

OutputDT.AsEnumerable.Where(
Function(x)
(
TypeVoucher.Contains(x(“Premium”).ToString) andAlso x(“Type Special”).ToString.Equals(“Yes”)
)
OrElse
(
x(“Premium”).ToString.Equals(“เงินคืนเข้าบัญชีเงินฝาก SCB”) or x(“Premium”).ToString.Equals(“เงินคืนเข้าบัตรเครดิต SCB”)
)
AndAlso
(
x(“Type Special”).ToString.Equals(“Yes”) AndAlso String.IsNullOrWhiteSpace(x(“Account No”).ToString)
)
).ToArray

————————————————————————

This is the expression you have mentioned in the value property

I will highlight the ) symbol which was missing

OutputDT.AsEnumerable.Where(
Function(x)
( - this bracket was missing
TypeVoucher.Contains(x(“Premium”).ToString) andAlso x(“Type Special”).ToString.Equals(“Yes”)
)
OrElse
(
x(“Premium”).ToString.Equals(“เงินคืนเข้าบัญชีเงินฝาก SCB”) or x(“Premium”).ToString.Equals(“เงินคืนเข้าบัตรเครดิต SCB”)
)
AndAlso
(
x(“Type Special”).ToString.Equals(“Yes”) AndAlso String.IsNullOrWhiteSpace(x(“Account No”).ToString)
)
).ToArray

If you can see the highlighted braces were missing and

Cheers

@fairymemay

1 Like

Glad it got resolved @fairymemay

1 Like

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