将字符串中的第一个字符与另一个字符串中的任何字符分组

发布于 2024-12-05 20:26:29 字数 1090 浏览 0 评论 0原文

在给定字符串列表的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵山崖 2024-12-12 20:26:29

尝试这样的事情:

    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();
    }

Try something like this:

    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();
    }
呆头 2024-12-12 20:26:29

如果您从所得到的列表中构建一个新字符串,它会解决您的问题吗?但这不会为您提供正确的组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文