C#中是否有类似于VB.NET的运算符? [英] Is there a VB.NET-Like operator in C#?
问题描述
我正在重写vb.net应用程序,但不能声称对vb很棒.我需要用C#编写等效代码:
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()
我到目前为止:
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();
如您所见,我不确定like运算符的含义是什么.我通过几个代码转换器进行了测试,他们不断抛出错误.
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.
推荐答案
要获得最等效的功能,请确保您的C#项目引用了Microsoft.VisualBasic程序集.
To get the most equivalent functionality ensure your C# project has a reference to the Microsoft.VisualBasic assembly.
例如,您可以直接在C#中使用VB.NET Like
运算符.
You can then directly use the VB.NET Like
operator from your C#, e.g.
LikeOperator.LikeString(gme.element_key_name, "*web", CompareMethod.Text);
请确保包含
using Microsoft.VisualBasic.CompilerServices;
这将获得最等效的功能,但是我认为这是一个小技巧.
This will get the most equivalent functionality, however would be what I consider a bit of a hack.
您的其他选择是利用 String.StartsWith
, String.EndsWith
, String.Contains
或正则表达式.
Your other options would be to make use of the String.StartsWith
, String.EndsWith
, String.Contains
or Regex.
这篇关于C#中是否有类似于VB.NET的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!