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

时间:2011-09-21 11:33:02

标签: 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 "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();
}

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

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

答案 1 :(得分:-1)

如果你要从List中建立一个新的字符串,它会解决你的问题吗,但这不会给你那些真实的群体。