将字符串中的第一个字符与另一个字符串中的任何字符分组
在给定字符串列表的 Linq2Sql 中,我需要一个查询来返回具有以字符串中的任何字符开头的系统的所有字符串。
这就是我想到的:(需要此功能的帮助)
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)
{
return
(from s in tblSystems
where
s.systemName != null && s.systemName.Length > 0 &&
characters.Contains(s.systemName[0])
select s).Any();
}
In Linq2Sql given a list of string, I need a query that returns back all the strings that has a system that starts with any of the characters in the string.
This is what I've come up with: (Needs help with this function)
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();
}
A single string check would be: (works as expected)
private bool StartsWithAny(string characters)
{
return
(from s in tblSystems
where
s.systemName != null && s.systemName.Length > 0 &&
characters.Contains(s.systemName[0])
select s).Any();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的事情:
Try something like this:
如果您从所得到的列表中构建一个新字符串,它会解决您的问题吗?但这不会为您提供正确的组。
Would it solve your problem if you would build a new string from the List what you get, but that wouldn't give you groups that's true.