private static String CheckLookupMatch(LanguageLookup languageLookup, String languageCode) { if (languageLookup.RootLanguages.Any(ll => ll.MappedLanguages.Any(ml => ml.LanguageCode.Equals(languageCode)))) { return languageLookup.RootLanguages.Single(ll => ll.MappedLanguages.Any(ml => ml.LanguageCode.Equals(languageCode))) .LanguageCode; } var rootLanguages = languageLookup.RootLanguages.Where(ll => ll.MappedLanguages.Any(ml => ml.LanguageCode.Contains("*"))); var enumerable = rootLanguages as IList<RootLanguage> ?? rootLanguages.ToList(); if ( enumerable.Any( rl => rl.MappedLanguages.Any(ml => ml.LanguageCode.Substring(0, 2).Equals(languageCode.Substring(0, 2))))) { return enumerable.First( rl => rl.MappedLanguages.Any(ml => ml.LanguageCode.Substring(0, 2).Equals(languageCode.Substring(0, 2)))) .LanguageCode; } return String.Empty; }
static void Main(string[] args) { var exampleLanguageLookup = new LanguageLookup { RootLanguages = new List<RootLanguage> { new RootLanguage { LanguageCode = "en-us", MappedLanguages = new List<MappedLanguage> { new MappedLanguage {LanguageCode = "en" }, new MappedLanguage {LanguageCode = "en-gb" }, new MappedLanguage {LanguageCode = "en-ca" } } } } }; var json = JsonConvert.SerializeObject(exampleLanguageLookup, Formatting.Indented); File.WriteAllText(String.Format("{0}\\{1}", Directory.GetCurrentDirectory(), "exampleLookup.json"), json); }