0

我想从 IGrouping 查询中获取结果并将其放入列表中。

我试图这样做如下:

实体类

public class WordRank
{

    public string Word { get; set; }
    public string WordScore { get; set; }
}

方法

     public void DisplayArticles()
    {
        var articles = this.articleRepository.TextMinerFindBy(this.view.Client, this.view.Brand, this.view.Project, this.view.Term, this.view.Channel, this.view.Begin, this.view.End, this.view.OnlyCategorized, this.view.UniquePosts);
        string snippets = string.Empty;

        foreach (var article in articles)
        {
            snippets = snippets + " " + article.Snippet;
        }

        Regex wordCountPattern = new Regex(@"[.,;:!?""\s-]");
        string snippetCollection = wordCountPattern.Replace(snippets, " ");

        var words = snippetCollection.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

        var groups = words.GroupBy(w => w);


        foreach (var item in groups)
        {
            this.view.Words.Add(item);
        }
    }

但不可能将项目分配给 IList。任何人都可以给我光吗?

谢谢

4

1 回答 1

7

编辑:好的,现在我们知道您要做什么(请参阅评论):

foreach (var group in groups)
{
    this.view.Words.Add(new WordRank { Word = group.Key,
                                       WordScore = group.Count() });
}

或者,如果您愿意将整个 替换为this.view.Wordsa List<WordRank>,请将整个底部位替换为:

this.view.Words = words.GroupBy(w => w)
                       .Select(new WordRank { Word = group.Key,
                                              WordScore = group.Count() })
                       .ToList();
于 2013-07-03T20:42:41.363 回答