2011-09-21 44 views
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(); 
} 

回答

-1

难道解决你的问题,如果你会从列表中建立一个新的字符串,但你不会给你的组是真的。

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

的'GetUsedStrings'功能在我的问题是与有trubble一个IM。 'StartsWithAny?'按预期工作 – Magnus

相关问题