VB.NET/C# Comparison Operator for checking a set of elements - as an IN statement for SQL

so in SQL you can do something like:

 WHERE title IN('title1','title2','title3')

to check for a set (assuming I'm using the correct phrase here). How can I do this in VB.NET/C#.NET?

Example:

 IF textMyTitle.text IN ("title1","title2","title 3") THEN
 'Take Action
 End If

Obviously, the IN part of this statement does not work ... What is the logical equivalent in .NET?

+3
source share
6 answers

Try the following:

Dim titles As New List(Of String)()
titles.Add("title1")
titles.Add("title2")
titles.Add("title3")

If titles.Contains(textMyTitle.text) Then
    // Do something here
End If
+4
source

There are several ways to do this. One of them is to create HashSetwith values ​​and check if it contains a string:

Dim values As new Hashet() { "title1", "title2", "title3" }
If values.Contains(textMyTitle.Text) Then
  ...
End If

, . HashSet O (1).

( 3.5 HashSet, Dictionary<string, byte>, .)

+3

:

if((new string[] {"title1", "title2", "title3"}).Contains(textMyTitle.Text)
{
    ...
}

, , . - ( , IEnumerable<string>), :

List<string> titles = new List<string>();

// add titles to the list

...

if(titles.Contains(textMyTitle.Text))
{
    ...
}
+1

IList - . .Net 2.0, Array IList < > . , IList:

string [] vars = new string [] { "title1", "title2", "title3" }; if (((IList) vars).Contains(textMyTitle.text)) {       // }

+1
source

Depending on the type of kit you have. If you can choose the type of set you could just set in HastSet ()

so your example would look like (C #)

var set = new System.Collections.Generic.HashSet<string>(){"title1","title2","title 3"};
if set.Contains(textMyTitle.text)
   //Take Action

List and arrays have similar methods, although they won't work for large sets as well (they might be better for small sets, but I haven't tried)

+1
source

Source: https://habr.com/ru/post/1743867/


All Articles