ruedi
ruedi

Reputation: 5545

How to use all array elements as where conditions in LINQ

I have a string array

Dim arrtest = New String() {"First", "Second", "Third"}

and a Datatable within a Dataset

_dataset.Table.AsEnumerable()

how is it possible to select all cases that meet the arrtest elements as where conditions?

The full (by feet) solution would look:

dim query = From dt In _dataset.Table.AsEnumerable()
where dt.Field1 = "First" or "Second" or "Third"

I thought of using the stringbuilder to generate a string with all the conditions seperated by "or" but was wondering if any of you Linq experts could help me finding a better solution within Linq?

Upvotes: 0

Views: 60

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460058

You can use Enumerable.Contains:

dim query = From row In _dataset.Tables(0).AsEnumerable()
            Where arrtest.Contains(row.Field(Of String)("Field1"))

Upvotes: 1

Related Questions