Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将iGroup转换为IList_C#_Linq - Fatal编程技术网

C# 将iGroup转换为IList

C# 将iGroup转换为IList,c#,linq,C#,Linq,我想从一个iGroup查询中获取结果并将其放入一个列表中 我试着这样做,如下所示: 实体类 public class WordRank { public string Word { get; set; } public string WordScore { get; set; } } 方法 public void DisplayArticles() { var articles = this.articleRepository.TextMin

我想从一个iGroup查询中获取结果并将其放入一个列表中

我试着这样做,如下所示:

实体类

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。 谁能给我点灯吗


谢谢编辑:好的,现在我们知道你想做什么了,请看评论:

foreach (var group in groups)
{
    this.view.Words.Add(new WordRank { Word = group.Key,
                                       WordScore = group.Count() });
}
或者,如果您愿意用列表替换整个this.view.Words,请将整个底部位替换为:

this.view.Words = words.GroupBy(w => w)
                       .Select(new WordRank { Word = group.Key,
                                              WordScore = group.Count() })
                       .ToList();

我不知道你的问题是什么。你在GroupBy中得到了什么?为什么不直接调用words.Distinct.ToList呢?我得到了单词列表以及它们在文本中出现的时间,所以我得到了这个结果,可以插入到IList单词中。谢谢Jon!this.view.Words的类型是IList Words{get;set;}。我已尝试实现您的sugested解决方案,但仍收到消息:无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IList”。你能帮我把这个换成现金吗?或者类似的?再次感谢!您需要为我们提供Culturala.Extraction.Core.Entities.Articles.Word的类定义‌​RankSure,类定义:namespace Cultura.Extraction.Core.Entities.Articles{public class WordRank{public string Word{get;set;}public string WordScore{get;set;}}@user1867530:你有几个组-你根本不清楚如何将一组单词转换成WordRank。我不需要将一个组转换成一个类WordRank,但是如果有任何方法,我可以在gridview中显示这个组,这将是非常有用的。