2011-09-21 18 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

相關問題