全部明确
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
IEnumerable<string> query = names
.Where (n => n.Contains ("a"))
我真正需要的是什么
string[] providers = { "gmail", "yahoo", "hotmail" , "something" };
并使用LINQ确定(不一定返回IEnumerable,bool会这样做)是否某些电子邮件地址某人@ 某事 .com来自此提供程序列表,即数组提供程序的元素是不区分大小写的子字符串电子邮件地址。
答案 0 :(得分:1)
您是要处理个人电子邮件还是收集em?
// individual
var email = ...;
var isValid = providers.Any(p => email.IndexOf(p, StringComparison.OrdinalIgnoreCase) != -1);
// collection
var emails = new[] { ... };
var validEmails = emails.Where(e => providers.Any(p => e.IndexOf(p, StringComparison.OrdinalIgnoreCase) != -1));
答案 1 :(得分:1)
你可以使用它。
string[] providers = { "gmail", "yahoo", "hotmail", "something" };
string inputEmail1 = "test@gmail.com";
string inputEmail2 = "test@gmail2.com";
var query = from n in providers
where inputEmail1.ToLower().Contains("@" + n + ".")
select n;
Console.WriteLine(query.Count()); //For inputeEmail1,Print 1. For inputeEmail2, it will print 0
答案 2 :(得分:1)
这将检查提供的电子邮件地址
string email = "me@hello.com";
string[] domains = new string[]{"hello", "goodbye"};
bool exists = new List<string>(domains).Exists(x => email.Conatains(string.Format(@"@{0}\.", x);
答案 3 :(得分:0)
我猜你想要对'@'和第一个'。'之间的文本进行不区分大小写的匹配。
所以,这是我的方法:
var providers = new []
{ "gmail", "yahoo", "hotmail", "something" };
var inputEmail = "test@gmail.com";
var match = inputEmail.Split('@')[1].Split('.')[0];
// You should probably expand this out to check for error in the input string.
Func<string, bool> isMatch =
p => String.Compare(match, p, true) == 0;
var query =
from p in providers
where isMatch(p)
select p;
var contains = query.Any();
我尝试将查询的详细信息删除到Func
,以使查询更加“简单”。