C# 将iGroup转换为IList
我想从一个iGroup查询中获取结果并将其放入一个列表中 我试着这样做,如下所示: 实体类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
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中显示这个组,这将是非常有用的。