9
這是一個小的拼字遊戲項目,我正在修補,並希望得到一些我可能做錯了什麼輸入。我有一個字母「詞典」,他們各自的分數和單詞列表。我的想法是找到每個單詞中的字母並將分數彙總在一起。LINQ'加入'期待一個平等,但我想使用'包含'
// Create a letter score lookup
var letterScores = new List<LetterScore>
{
new LetterScore {Letter = "A", Score = 1},
// ...
new LetterScore {Letter = "Z", Score = 10}
};
// Open word file, separate comma-delimited string of words into a string list
var words = File.OpenText("c:\\dictionary.txt").ReadToEnd().Split(',').ToList();
// I was hoping to write an expression what would find all letters in the word (double-letters too)
// and sum the score for each letter to get the word score. This is where it falls apart.
var results = from w in words
join l in letterScores on // expects an 'equals'
// join l in letterScores on l.Any(w => w.Contains(
select new
{
w,
l.Score
};
任何幫助將不勝感激。 謝謝。
謝謝!就是這樣。但是,在最後兩個代碼塊中,它看起來好像您打算使用* scoreDictionary *而不是* letterScores *。 –
@安迪:謝謝,修正:) –