11

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.

Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
Robert
  • 4,084
  • 8
  • 39
  • 81
  • What is the source you're querying? `.Where(x => x.EndsWith("email");` might be appropriate. – Jeroen Vannevel Jul 17 '14 at 20:14
  • There's no `Like` operator in C#, in this case you can use `String.EndsWith`(if this is `Linq-To-Objects`), otherwise you need to use regex or `SqlMethods.Like`( if it's `Linq-To-Sql` ). – Tim Schmelter Jul 17 '14 at 20:14
  • This question is not a duplicate, this concerns converting the VB.NET `Like` operator to a C# equivalent whereas the other question is the SQL `LIKE` operator, different things... – Lukazoid Jul 17 '14 at 20:18
  • @Robert: it's at the top of your post (perhaps you need to refresh) – Jeroen Vannevel Jul 17 '14 at 20:20
  • This is not a duplicate as stated, for the reason Lukazoid mentions. This is about a different operator altogether. – Holf Jul 17 '14 at 20:24
  • 1
    It is possible to achieve the same in C# but you don't get the same syntactic sugar, so it's a bit ugly: `where LikeOperator.LikeString(mySourceString, "*Something*", CompareMethod.Text)` It's in the `Microsoft.VisualBasic.CompilerServices` namespace. Further info here: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.compilerservices.likeoperator.likestring(v=vs.110).aspx – Holf Jul 17 '14 at 20:26

3 Answers3

20

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.

Lukazoid
  • 17,448
  • 3
  • 57
  • 79
5

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

Shimmy Weitzhandler
  • 92,920
  • 119
  • 388
  • 596
Amir Popovich
  • 26,624
  • 8
  • 44
  • 88
0

I think Regex is the best option. Since the Like operation supports *,?, # and [], I think there can be complex patterns possible which can be matched easily using the Regex library. For eg. the following lines will return true.

"aBBBa" Like "a*a" "ajhfgZ1" Like "*Z[12]"

Now it depends on your application. If you are just using it to match a simple hardcoded string , you can directly use String.Contains, String,StartsWith or String.EndsWith but for complex matching use Regex for the best results.

ketan Shah
  • 88
  • 8
  • 1
    You should probably add the actual regexes that you suggest. If you're trying to say `*Z[12]` is a regular expression, that's wrong. `a*a` also doesn't match `aBBBa`--it only matches 0 or more `a`s then another `a`. You may have meant `a.*a` and `Z[12]`. (Or you may have only been trying to demonstrate Like syntax, but this is unclear to me.) – 31eee384 Jan 28 '16 at 15:33
  • hiii the a*a does match aBBBa . And the the the "Like" operation matches patterns with strings, so the pattern part of the equation does support * , ? and etc. and *Z[12] can be converted to a regular expression i meant, but for LIKE operation that is the way it can be used. More info on this is at https://msdn.microsoft.com/en-us/library/swf8kaxw.aspx Anyways feel free to correct me if I'm wrong. Thats how i'll learn :) – ketan Shah Jan 28 '16 at 16:34
  • 2
    Ok, my complaint is really just that it's unclear what your example means. `which can be matched easily using the Regex library. For eg. the following lines will return true.` leads me to believe the next few lines will have regular expressions. Pointing out that you're giving the examples just to show that Like is complex would clear it up I think. – 31eee384 Jan 28 '16 at 17:54