コード例 #1
0
    private static void ImportMountain(JToken mountainObj)
    {
        var context = new MountainsContext();
        var mountain = new Mountain();

        // Parse mountain
        if (mountainObj["mountainName"] == null)
        {
            throw new Exception("Missing mountain name");
        }
        mountain.Name = mountainObj["mountainName"].Value<string>();

        // Parse peaks
        var peaks = mountainObj["peaks"];
        if (peaks != null)
        {
            foreach (var peak in peaks)
            {
                if (peak["peakName"] == null)
                {
                    throw new Exception("Missing peak name");
                }
                string peakName = peak["peakName"].Value<string>();

                if (peak["elevation"] == null)
                {
                    throw new Exception("Missing peak elevation");
                }
                int elevation = peak["elevation"].Value<int>();

                mountain.Peaks.Add(new Peak() { Name = peakName, Elevation = elevation});
            }
        }

        // Parse countries
        var countryNames = mountainObj["countries"];
        if (countryNames != null)
        {
            foreach (var countryName in countryNames)
            {
                var countryCode =
                    new string(countryName.Value<string>().Take(2).ToArray()).ToUpper();
                var country = context.Countries.Find(countryCode);
                if (country == null)
                {
                    country = new Country() { Code = countryCode, Name = countryName.Value<string>() };
                }
                mountain.Countries.Add(country);
            }
        }

        // Persist the mountain in the database
        context.Mountains.Add(mountain);
        context.SaveChanges();
    }
コード例 #2
0
 static void Main()
 {
     var context = new MountainsContext();
     var mountains = context.Mountains.Select( m =>
         new {
             m.Name,
             Countries = m.Countries.Select(c => c.Name),
             Peaks = m.Peaks.Select(p => new { p.Name, p.Elevation })
         });
     foreach (var mountain in mountains)
     {
         Console.WriteLine(
             "{0}, countries: {1}, peaks: {2}",
             mountain.Name,
             string.Join(", ", mountain.Countries),
             string.Join(", ", mountain.Peaks.Select(
                 p => p.Name + " (elevation " + p.Elevation + ")")));
     }
 }
コード例 #3
0
        static void Main()
        {
            var context   = new MountainsContext();
            var mountains = context.Mountains.Select(m =>
                                                     new {
                m.Name,
                Countries = m.Countries.Select(c => c.Name),
                Peaks     = m.Peaks.Select(p => new { p.Name, p.Elevation })
            });

            foreach (var mountain in mountains)
            {
                Console.WriteLine(
                    "{0}, countries: {1}, peaks: {2}",
                    mountain.Name,
                    string.Join(", ", mountain.Countries),
                    string.Join(", ", mountain.Peaks.Select(
                                    p => p.Name + " (elevation " + p.Elevation + ")")));
            }
        }