public static async Task <Phrase> GetRandomHskPhraseByLevel(int hskLevel)
 {
     return(await Task.Run(() =>
     {
         using (var context = new LanguageLearningModel())
         {
             Log.DebugFormat("Loading random HSK Phrases found for level {0}.", hskLevel);
             HskPhrase randomHskPhrase = context.HskPhrases.Include(x => x.MeasureWords).OrderBy(phrase => Guid.NewGuid()).FirstOrDefault(x => x.HskLevel == hskLevel);
             Log.DebugFormat("Completed loading random HSK Phrases found for level {0}.", hskLevel);
             return randomHskPhrase;
         }
     }));
 }
        public async Task ImportPhrasesAsync()
        {
            await Task.Run(() =>
            {
                using (var sr = new StringReader(Resources.Hsk6))
                    using (var csv = new CsvReader(sr))
                    {
                        while (csv.Read())
                        {
                            string chineseWord       = GetColumn(csv, "Word");
                            string pinyin            = GetColumn(csv, "Pronunciation");
                            string englishDefinition = GetColumn(csv, "Definition");
                            int hskLevel             = ParseHskLevel(csv);

                            string[] splitEnglish = englishDefinition.Split(new[] { "CL:" }, StringSplitOptions.None);
                            string english        = splitEnglish[0].Trim();
                            var phrase            = new HskPhrase {
                                Hanzi = chineseWord, Pinyin = pinyin, English = english, HskLevel = hskLevel
                            };

                            List <MeasureWord> measureWords = FindMeasureWords(splitEnglish).ToList();

                            using (var context = new LanguageLearningModel())
                            {
                                phrase.Tags         = new List <Tag>();
                                phrase.MeasureWords = new List <MeasureWord>();

                                Log.DebugFormat("Adding phrase [{0}]", phrase.Pinyin);

                                AddPhrase(context, phrase);

                                var tag = new Tag {
                                    Name = $"HSK{hskLevel}"
                                };

                                AddElementToSet(context, tag, phrase.Tags, x => x.Name == tag.Name);

                                foreach (MeasureWord measureWord in measureWords)
                                {
                                    AddElementToSet(context, measureWord, phrase.MeasureWords, mw => mw.Hanzi == measureWord.Hanzi);

                                    context.SaveChanges();
                                }

                                context.SaveChanges();
                            }
                        }
                    }
            });
        }