2010-12-01 6 views
0

this postを取って自分の目的に合わせて調整しようとしていますが、どうしたらいいか分かりません。ここでRegexを使ってLinq Objectの単語頻度(カウント)を取得

が始まるクエリです:

 string input = sb.ToString(); 
      string[] keywords = new[] { "i","be", "with", "are", "there", "use", "still", "do","out", "so", "will", "but", "if", "can", "your", "what", "just", "from", "all", "get", "about", "this","t", "is","and", "the", "", "a", "to", "http" ,"you","my", "for", "in", "of", "ly" , "com", "it", "on","s", "that", "bit", "at", "have", "m", "rt", "an", "was", "as", "ll", "not", "me" }; 
      Regex regex = new Regex("\\w+"); 
var stuff = regex.Matches(input) 
       .OfType<Match>() 
       .Select(c => c.Value.ToLowerInvariant()) 
       .Where(c => !keywords.Contains(c)) 
       .GroupBy(c => c) 
       .OrderByDescending(c => c.Count()) 
       .ThenBy(c => c.Key); 

しかし、私は私のデータベースに格納することができるように、各キー値のCOUNT(周波数)だけでなく、値そのものを取得できるようにしたいと思います。

foreach (var item in stuff) 
      { 
       string query = String.Format("INSERT INTO sg_top_words (sg_word, sg_count) VALUES ('{0}','{1}')", item.Key, item.COUNT???); 
       cmdIns = new SqlCommand(query, conn); 
       cmdIns.CommandType = CommandType.Text; 
       cmdIns.ExecuteNonQuery(); 
       cmdIns.Dispose(); 
      } 

おかげで

答えて

3

クエリはあなたが後にしているほぼ何あり、この微調整はそれを行う必要があると仮定:

var stuff = regex.Matches(input) 
    .Cast<Match>() // We're confident everything will be a Match! 
    .Select(c => c.Value.ToLowerInvariant()) 
    .Where(c => !keywords.Contains(c)) 
    .GroupBy(c => c) 
    .Select(g => new { Word = g.Key, Count = g.Count() }) 
    .OrderByDescending(g => g.Count) 
    .ThenBy(g => g.Word); 

今シーケンスはKeyで、匿名型になりますおよびCountプロパティ。

データベースに挿入するだけの場合は、実際に結果を注文する必要はありますか?

var stuff = regex.Matches(input) 
    .Cast<Match>() // We're confident everything will be a Match! 
    .Select(c => c.Value.ToLowerInvariant()) 
    .Where(c => !keywords.Contains(c)) 
    .GroupBy(c => c) 
    .Select(g => new { Word = g.Key, Count = g.Count() }); 
+0

これで、カウント値が保存されたので、もう注文する必要はありません。 :) – discorax

関連する問題