2012-01-30 153 views
1

這可能已經被處理了,所以我提前爲此道歉。LINQ GroupBy

無論如何,這是我的有點做作的例子,我希望它會在我的問題:

說我們有這些類

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

class Sentence 
{ 
    public List<string> words; 
} 

然後我們設置了兩個列表:

List<Sentence> sentences = GetSomeSentences(); 
List<WordExamples> wordExamples = 
    GetSomeWords().Select(w=>new WordExamples(w)); 

正如您所看到的WordExamples列表包含的單詞示例不完整,因爲它們沒有實例化sentencesWithWordList。

所以我需要的是一些整齊的Linq,它會設置它。即例如: foreach wordExample獲取包含該單詞的句子的子集並將其分配給sentencesWithWord。 (withouth的嵌套的循環,即是)

編輯: 添加公共訪問修飾符

+0

當一個私有字段沒有用在聲明它的類中時,如何將任何代碼賦給'sentencesWithWord'? – svick 2012-01-30 14:33:15

+0

@svick,是的,它是未經破解的僞代碼 – 2012-01-30 15:03:37

回答

0

由於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(); 
} 
+1

該代碼關閉循環變量,並且無法正常工作。 – Ani 2012-01-30 14:20:48

+0

@Ani:代碼編譯並運行得很好。產出也如預期。 – 2012-01-30 14:25:11

+0

@DanielHilgarth,輸出不是預期的。所有成員'wordExamples'都會根據最後一個成員設置'sentencesWithWord'。 – svick 2012-01-30 14:41:09

2

這不是真的清楚你以後,但我嫌疑你想:

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

這基本上就像一個Lookup的元素,你要知道...

無論如何,與在地方,你可以使用:

var wordExamples = from sentence in sentences 
        from word in sentence.Words 
        group sentence by word into g 
        select new WordExample(g.Key, g.ToList()); 
+0

對不起,不夠清楚。我沒有按照你的例子... WordExamples是一個類,你的代碼似乎假設不同。 – 2012-01-30 15:02:13

+0

@tycomiplex:我的代碼沒有*使用*一個WordExamples類,因爲它似乎並沒有真正添加任何值 - 並且您還沒有提供任何方式來獲取它的列表。 (句子及其詞組成員當然也是如此......) – 2012-01-30 15:03:59

+0

正如我所說的,這是一個人爲的例子。我也質疑它的價值,不幸的是,在我的現實世界的情況下,我無法解決這個問題,這就是爲什麼我將它包括在內的原因。修正了訪問修飾符,謝謝。 – 2012-01-30 15:13:10

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

不要忘記設置正確的訪問修飾符。

0

看起來你正在重新發明一個ILookup。

ILookup<string, Sentence> examples = GetSentences() 
    .SelectMany(sentence => sentence.words, (sentence, word) => new {sentence, word}) 
    .ToLookup(x => x.word, x => x.sentence); 
+0

對,WordExamples類的工作原理類似於查找,但並未發明任何東西,只是試圖解決一個工作示例,其中類是複雜的dto – 2012-01-30 15:29:32