C# 林克群比结束

C# 林克群比结束,c#,linq,C#,Linq,这可能已经处理好了,所以我提前为此道歉 无论如何,这是我做作的例子,我希望它能让我的问题得到理解: 假设我们有这些课程 class WordExamples { public string word; public List<Sentence> sentencesWithWord; //constructor public WordExamples(string word) { this.word = word; } } class Se

这可能已经处理好了,所以我提前为此道歉

无论如何,这是我做作的例子,我希望它能让我的问题得到理解:

假设我们有这些课程

class WordExamples 
{
   public string word;
   public List<Sentence> sentencesWithWord;
   //constructor
   public WordExamples(string word) {
      this.word = word;
   }
}

class Sentence 
{
   public List<string> words;
}
classwordexamples
{
公共字符串;
公共列表语句;
//建造师
公共单词示例(字符串单词){
这个单词=单词;
}
}
类别句
{
公共词汇表;
}
然后我们设置了两个列表:

List<Sentence> sentences = GetSomeSentences();
List<WordExamples> wordExamples = 
    GetSomeWords().Select(w=>new WordExamples(w));
列出句子=getsome句子();
列表单词示例=
GetSomeWords()。选择(w=>newwordexamples(w));
正如您所看到的,单词示例列表包含不完整的单词示例,因为它们没有实例化句子和单词列表

所以我需要的是一些整洁的Linq来设置这个。例如: foreach wordExample获取包含单词的句子子集,并将其分配给句子。(没有嵌套的循环是)

编辑:
添加公共访问修饰符

不太清楚您的目标,但我怀疑您想要:

foreach (var example in wordExamples)
{
    Console.WriteLine("Word {0}", example.Key);
    foreach (var sentence in example)
    {
        // I assume you've really got the full sentence here...
        Console.WriteLine("  {0}", string.Join(" ", sentence.Words));
    }
}
编辑:如果您确实需要
WordExamples
类,您可以:

public class WordExamples 
{
   public string Word { get; private set; }
   public List<Sentence> SentencesWithWord { get; private set; }

   public WordExamples(string word, List<Sentences> sentences) {
      Word = word;
      // TODO: Consider cloning instead
      SentencesWithWord = sentences;
   }
}

由于LINQ是一种查询语言,而不是赋值语言,因此应该使用循环:

List<WordExamples> wordExamples = GetSomeWords().Select(w=>new WordExamples(w))
                                                .ToList();


foreach(var wordExample in wordExamples)
{
    wordExample.sentencesWithWord
        = sentences.Where(x => x.words.Contains(wordExample.word)).ToList();
}
List wordExamples=GetSomeWords()。选择(w=>newwordexamples(w))
.ToList();
foreach(wordExamples中的var wordExample)
{
单词示例。句子swithword
=句子。其中(x=>x.words.Contains(wordExample.word)).ToList();
}
IEnumerable wordExamples=GetSomeWords()。选择(w=>
{
变量示例=新单词示例(w);
examples.sentenceswitword=句子。其中(s=>s.words.Any(sw=>sw==w)).ToList();
返回示例;
}
);

别忘了设置正确的访问修饰符。

看起来你正在发明一个ILookup

ILookup<string, Sentence> examples = GetSentences()
  .SelectMany(sentence => sentence.words, (sentence, word) => new {sentence, word} )
  .ToLookup(x => x.word, x => x.sentence);
ILookup examples=get句子()
.SelectMany(句子=>句子.words,(句子,单词=>新的{句子,单词})
.ToLookup(x=>x.word,x=>x.句子);

此代码关闭循环变量,无法正常工作。@Ani:代码编译并运行正常。输出也是预期的。@DanielHilgarth,输出不是预期的。所有成员
wordExamples
都将根据上一个设置
sentencesWithWord
。@svick:的确,我在这里复制了一个旧版本。如果没有
ToList
,它甚至无法编译。固定的。我在做测试时自动修复了它,但忘了把它放回这里。。。很抱歉。当声明它的类中没有使用私有字段时,任何代码如何分配给
sentencesWithWord
。我不以你为榜样。。。WordExamples是一个类,您的代码的假设似乎有所不同。@tycomiplex:我的代码没有使用WordExamples类,因为它似乎没有实际添加任何值,而且您也没有提供任何方法来获取其中的列表。(当然,句子及其单词member也是如此……)对,就像我说的,这是一个人为的例子。我也质疑它的价值,不幸的是,在我的现实世界中,这是我无法消除的,这就是为什么我包括它。修复了访问修饰符,谢谢。@tycomiplex:你真的有公共字段吗?哎呀。不过,我会根据我的建议进行编辑。谢谢你的Linq代码,它在修复了句子中从word开始的第二行之后工作得非常完美。WordsRight,WordExamples类的工作方式类似于查找,但没有发明任何东西,只是尝试解决一个工作示例,其中该类是一个复杂的dto
IEnumerable<WordExamples> wordExamples = GetSomeWords().Select(w=>
{
   var examples = new WordExamples(w);
   examples.sentencesWithWord = sentences.Where(s => s.words.Any(sw => sw == w)).ToList();
   return examples;
}
);
ILookup<string, Sentence> examples = GetSentences()
  .SelectMany(sentence => sentence.words, (sentence, word) => new {sentence, word} )
  .ToLookup(x => x.word, x => x.sentence);