C# 将字符串中的第一个字符按另一个字符串中的任何字符分组
在给定字符串列表的Linq2Sql中,我需要一个查询,该查询返回系统以字符串中的任何字符开头的所有字符串 这就是我想到的:这个函数需要帮助C# 将字符串中的第一个字符按另一个字符串中的任何字符分组,c#,linq-to-sql,C#,Linq To Sql,在给定字符串列表的Linq2Sql中,我需要一个查询,该查询返回系统以字符串中的任何字符开头的所有字符串 这就是我想到的:这个函数需要帮助 void Main() { var strings = new List<string>(){"ABCDE", "FGHIJ", "KLMNO"}; var values = GetUsedStrings(strings); /* * In my case expected to return strings "
void Main()
{
var strings = new List<string>(){"ABCDE", "FGHIJ", "KLMNO"};
var values = GetUsedStrings(strings);
/*
* In my case expected to return strings "ABCDE" and "KLMNO"
* since there exists Systems that starts
* with any of the characters in those strings.
*/
values.Dump();
}
public IList<string> GetUsedStrings(IList<string> strings)
{
var q = from s in tblSystems
where s.systemName != null && s.systemName.Length > 0
group s by s.systemName[0] into g //Somehow need to group by the characters strings list?
select g.Key;
return q.ToList();
}
如果你想从列表中生成一个新字符串,但这并不能为你提供正确的分组,这会解决你的问题吗
private bool StartsWithAny(string characters)
{
string aa = @"if exists(select * from tblSystems where
systemName is not null and LEN(systemName)>0";
for (int i = 0; i < characters.Length; i++)
{
aa += " and SUBSTRING([login],1,1) = '" + characters[i] + "'";
}
aa+=")";
return db.ExecuteQuery<bool>(aa).Single();
}
我问题中的GetUsedStrings函数就是我正在使用的trubble函数。有开始吗?工作如期进行
private bool StartsWithAny(string characters)
{
string aa = @"if exists(select * from tblSystems where
systemName is not null and LEN(systemName)>0";
for (int i = 0; i < characters.Length; i++)
{
aa += " and SUBSTRING([login],1,1) = '" + characters[i] + "'";
}
aa+=")";
return db.ExecuteQuery<bool>(aa).Single();
}