get word frequency (count) as property in Linq Object using Regex

So I'm trying to take this post and tweak it for my own purposes but I can't figure out how.

Here is a starting Query:

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

But I would like to be able to get the COUNT (Frequency) of each Key value as well as the value itself so that I can store it in my database.

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

Thanks

Answers


Assuming the query is nearly what you're after, this tweak should do it:

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

Now the sequence will be of an anonymous type, with Key and Count properties.

Do you really need to order the results though, if you're just inserting them into a database? Could you just use this:

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

Need Your Help

JavaFX Dialog location relativ to window (Canvas) position

java scala javafx

I was wondering if there is a (simple) way to place a JavaFX Dialog relative to another GUI-Component.

ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value

python django heroku django-postgresql

I am in the midst of setting up my django project on heroku. I have been following the documentation, but when I foreman start I receive an error that I can't quite figure out. I have set up my eng...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.