Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a VB.NET-Like operator in C#?

Tags:

c#

vb.net

linq

I am rewriting a vb.net app and I can't claim to be great with vb. I need to write this equivilent in C#:

Dim bigList = (From gme In dtx.gmc_message_elements 
              Where gme.element_key_name Like "*email" _
              Or gme.element_key_name Like "*web" 
              Or gme.element_key_name Like "*both" _
              Select gme.element_key_name Distinct).ToList()

I have so far:

var bigList = (from gme in dtx.gmc_message_elements 
               where gme.element_key_name Like "*email" 
               || gme.element_key_name Like "*web" 
               || gme.element_key_name Like "*both" 
               select gme.element_key_name).FirstOrDefault().ToList();

As you can see I am not sure what the equivalent of the like operator is. I ran this through a couple code converters and they constantly threw errors.

like image 591
Robert Avatar asked Jul 17 '14 20:07

Robert


2 Answers

To get the most equivalent functionality ensure your C# project has a reference to the Microsoft.VisualBasic assembly.

You can then directly use the VB.NET Like operator from your C#, e.g.

LikeOperator.LikeString(gme.element_key_name, "*web", CompareMethod.Text);

Be sure to include the

using Microsoft.VisualBasic.CompilerServices;

This will get the most equivalent functionality, however would be what I consider a bit of a hack.

Your other options would be to make use of the String.StartsWith, String.EndsWith, String.Contains or Regex.

like image 53
Lukazoid Avatar answered Oct 28 '22 08:10

Lukazoid


Use StartsWith or EndsWith or Contains static methods of string based on your needs.

like image 39
Amir Popovich Avatar answered Oct 28 '22 08:10

Amir Popovich