MVC Linq查询中的返回时间序列

时间:2019-02-16 18:36:56

标签: c# entity-framework linq

仅仅是从Wolfram语言背景的C#开始。试图建立一个网站,显示从天气(和其他)传感器收集的遥测数据。我希望能够将包含datetime2double数据类型的SQL Server数据库中的数据传递给JSON格式的视图。构建包含DateTime格式的集合时遇到麻烦。

模型 (DLWeatherMain)

using System.ComponentModel.DataAnnotations;

namespace WeatherViewer.Models
{
    public partial class DlMainWeather
    {
        [DataType(DataType.DateTime)]
        [Required]
        public DateTime TmStamp { get; set; }
        public double? AirTempAvg { get; set; }
    }
 }

(TelemetryDataContext)

    using System;
    using Microsoft.EntityFrameworkCore;

    namespace WeatherViewer.Models
    {
        public partial class TelemetryDataContext : DbContext
        {
            public TelemetryDataContext()
            {
            }

            public TelemetryDataContext(DbContextOptions<TelemetryDataContext> options)
                : base(options)
            {
            }

            public virtual DbSet<DlMainWeather> DlMainWeather { get; set; }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                    optionsBuilder.UseSqlServer("connectionstring");
                }
            }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.HasAnnotation("ProductVersion", "2.2.2-servicing-10034");


                modelBuilder.Entity<DlMainWeather>(entity =>
                {
                    entity.HasKey(e => new { e.TmStamp,})
                        .HasName("PK__DL_MAIN___B3E4C26B9637DE80");

                    entity.ToTable("DL_MAIN_Weather");

                    entity.Property(e => e.AirTempAvg).HasColumnName("AirTemp_Avg");

                });

            }
        }
    }

(WeatherDataAccessLayer)

using System;
using System.Collections.Generic;
using System.Linq;


namespace WeatherViewer.Models
{
    public class WeatherDataAccessLayer
    {
        TelemetryDataContext db = new TelemetryDataContext();
        public IEnumerable<DlMainWeather> GetTemperatures(double backtime)
        {
            IEnumerable<DlMainWeather> temparray = db.DlMainWeather
                            .Where(dt => dt.TmStamp.Date > DateTime.Now.AddHours(-backtime))
                            .Select(dt => new { dt.TmStamp.Date, dt.AirTempAvg });
            return temparray;
        }
    }
}

控制器

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using WeatherViewer.Models;

namespace WeatherViewer.Controllers
{

    public class WeatherController : Controller
    {
        WeatherDataAccessLayer weatherobj = new WeatherDataAccessLayer();

        public IActionResult Index()
        {
            return View();
        }

        public ActionResult TemperatureSummary()
        {
            return PartialView("_TemperatureReport");
        }

        public JsonResult TemperatureReturn()
        {
            List<DlMainWeather> temparray = new List<DlMainWeather>();
            temparray = weatherobj.GetTemperatures(12).ToList();
            return new JsonResult(temparray);
        }
    }
}

因此,我的问题出在DataAccessLayer。从DAL传递temparray时,在转换为IEnumerable时引发错误。将时间序列(或通常为混合数据类型数组)从Entity Framework作为JSON传递到View的最佳方法是什么?

也:TmStamp可以转换为unix时间,如果这样可以使问题更容易解决。

0 个答案:

没有答案