• EF接收数据通用实体模型


    如何接收EF查询的不同实体模型

            /// 实时天气
            /// </summary>
            /// <param name="selectDate"></param>
            /// <param name="type">数据类型:1地上;2地下</param>
            /// <param name="minuteType">时间类型:1 10分钟;2 30分钟</param>
            /// <returns></returns>
            public ApiResult GetWeatherHourly(int type, int minuteType)
            {
                ApiResult result = new ApiResult();
                result.success = true;
                IQueryable data;
                //var endDate = DateTime.Now;
                //var endDate = new DateTime(2020,11,30,15,15,16);
                //var endDate = new DateTime(2020, 11, 18, 13, 15, 16);
                var endDate = db.T_AWS10min.Select(d => d.TIMESTAMP).Max();
                var startDate = endDate.AddHours(-24);
                //var startDate = endDate.Date;
                //var minuteType =1;
                if (type == 1)
                {
                    if (minuteType == 1)
                    {
                        data = db.T_AWS10min.Where(d => d.TIMESTAMP >= startDate && d.TIMESTAMP <= endDate).Select(d => d);
                    }
                    else if (minuteType == 2)
                    {
                        data = db.T_AWS30min.Where(d => d.TIMESTAMP >= startDate && d.TIMESTAMP <= endDate).Select(d => d);
                    }
                    else
                    {
                        //data = db.V_AWSHour.Where(d => (d.TIMESTAMP == startDate.Date && d.Hour >= startDate.Hour) || (d.TIMESTAMP == endDate.Date && d.Hour <= endDate.Hour)).Select(d => d).OrderBy(d => new { d.TIMESTAMP, d.Hour });
                        var sql = @"SELECT     
                                    [Project1].[TIMESTAMP] AS [TIMESTAMP],     [Project1].[Hour] AS [Hour],     [Project1].[TA_Avg] AS [TA_Avg],     [Project1].[TA_Avg_Max] AS [TA_Avg_Max],     [Project1].[TA_Avg_Min] AS [TA_Avg_Min],     [Project1].[RH_Avg] AS [RH_Avg],     [Project1].[Rain_Tot] AS [Rain_Tot],     [Project1].[WS_Avg] AS [WS_Avg],     [Project1].[Press_Avg] AS [Press_Avg],     [Project1].[Sun_Hour_Tot] AS [Sun_Hour_Tot],     [Project1].[DR_Avg] AS [DR_Avg],     [Project1].[UR_Avg] AS [UR_Avg],     [Project1].[DLR_Avg] AS [DLR_Avg],     [Project1].[ULR_Avg] AS [ULR_Avg],     [Project1].[cnr4_T_C_Avg] AS [cnr4_T_C_Avg],     [Project1].[cnr4_T_K_Avg] AS [cnr4_T_K_Avg],     [Project1].[Rn_Avg] AS [Rn_Avg],     [Project1].[albedo_Avg] AS [albedo_Avg],     [Project1].[GS_2cm_Avg] AS [GS_2cm_Avg],     [Project1].[GS_5cm_Avg] AS [GS_5cm_Avg],     [Project1].[PAR_Avg] AS [PAR_Avg],     [Project1].[TargTempC_Avg] AS [TargTempC_Avg],     [Project1].[SBTempC_Avg] AS [SBTempC_Avg],     [Project1].[UV_Avg] AS [UV_Avg],     [Project1].[dir_Ra_Avg] AS [dir_Ra_Avg],     [Project1].[Depth_Avg] AS [Depth_Avg]    
                                    FROM ( 
                                            SELECT  
                                            ISNULL(CONVERT(date,a.TIMESTAMP,121), '1840-06-06') as TIMESTAMP, 
                                            ISNULL(datepart(hh,a.TIMESTAMP),25)as Hour,
                                            avg(a.TA_Avg) AS TA_Avg,
                                            Max(a.TA_Avg) AS TA_Avg_Max,
                                            Min(a.TA_Avg) AS TA_Avg_Min,
                                            avg(a.RH_Avg) AS RH_Avg,
                                            sum(a3.Rain_Tot) AS Rain_Tot,
                                            avg(a3.WS_Avg) AS WS_Avg,
                                            avg(a.Press_Avg) AS Press_Avg,
                                            sum(a.Sun_Hour_Tot)/60 AS Sun_Hour_Tot,
                                            avg(a.DR_Avg) AS DR_Avg,
                                            avg(a.UR_Avg) AS UR_Avg,
                                            avg(a.DLR_Avg) AS DLR_Avg,
                                            avg(a.ULR_Avg) AS ULR_Avg,
                                            avg(a.cnr4_T_C_Avg) AS cnr4_T_C_Avg,
                                            avg(a.cnr4_T_K_Avg) AS cnr4_T_K_Avg,
                                            avg(a.Rn_Avg) AS Rn_Avg,
                                            avg(a.albedo_Avg) AS albedo_Avg,
                                            avg(a.GS_2cm_Avg) AS GS_2cm_Avg,
                                            avg(a.GS_5cm_Avg) AS GS_5cm_Avg,
                                            avg(a.PAR_Avg) AS PAR_Avg,
                                            avg(a.TargTempC_Avg) AS TargTempC_Avg,
                                            avg(a.SBTempC_Avg) AS SBTempC_Avg,
                                            avg(a.UV_Avg) AS UV_Avg,
                                            avg(a.dir_Ra_Avg) AS dir_Ra_Avg,
                                            avg(a.Depth_Avg) AS Depth_Avg
                                            FROM   dbo.T_AWS10min as a left join dbo.T_AWS30min a3 on a.TIMESTAMP=a3.TIMESTAMP
                                            where (a.[TIMESTAMP] >= @startDate) AND (a.[TIMESTAMP] <= @endDate)
                                            GROUP BY  CONVERT(date,a.TIMESTAMP,121),datepart(hh,a.TIMESTAMP)
                                        ) as Project1  
                                    order by Project1.TIMESTAMP,Project1.Hour";
                        var sqlParams = new SqlParameter[]
                        {
                                new SqlParameter("@startDate",System.Data.SqlDbType.DateTime) {Value= startDate},
                                new SqlParameter("@endDate",System.Data.SqlDbType.DateTime) {Value= endDate}
                        };
                        var data1 = db.Database.SqlQuery<V_AWSHour>(sql, sqlParams).ToList();
                        //.OrderBy(d => new { d.TIMESTAMP, d.Hour });//报错:必须至少有一个对象实现 IComparable。
                        result.data = new { data1 };
                        return result;
                    }
                }
                else
                {
                    if (minuteType == 1)
                    {
                        data = db.T_TMS10min.Where(d => d.TIMESTAMP >= startDate && d.TIMESTAMP <= endDate)
                            .Select(d => d);
                        //.Select(d => new
                        //{
                        //    d.ID,
                        //    d.TIMESTAMP,
                        //    d.RECORD,
                        //    d.TS_0cm_Avg,
                        //    d.MS_5cm_Avg,
                        //    d.EC_5cm_Avg,
                        //    d.TS_5cm_Avg,
                        //    d.MS_10cm_Avg,
                        //    d.EC_10cm_Avg,
                        //    d.TS_10cm_Avg,
                        //    d.MS_20cm_Avg,
                        //    d.EC_20cm_Avg,
                        //    d.TS_20cm_Avg,
                        //    //d.MS_40cm_Avg,
                        //    //d.EC_40cm_Avg,
                        //    //d.TS_40cm_Avg,
                        //    d.MS_60cm_Avg,
                        //    d.EC_60cm_Avg,
                        //    d.TS_60cm_Avg,
                        //    d.MS_80cm_Avg,
                        //    d.EC_80cm_Avg,
                        //    d.TS_80cm_Avg,
                        //    d.MS_100cm_Avg,
                        //    d.EC_100cm_Avg,
                        //    d.TS_100cm_Avg
                        //});
                    }
                    else if (minuteType == 2)
                    {
                        data = db.T_TMS30min.Where(d => d.TIMESTAMP >= startDate && d.TIMESTAMP <= endDate)
                            .Select(d => d);
                        //.Select(d => new
                        //{
                        //    d.ID,
                        //    d.TIMESTAMP,
                        //    d.RECORD,
                        //    d.TS_0cm_Avg,
                        //    d.MS_5cm_Avg,
                        //    d.EC_5cm_Avg,
                        //    d.TS_5cm_Avg,
                        //    d.MS_10cm_Avg,
                        //    d.EC_10cm_Avg,
                        //    d.TS_10cm_Avg,
                        //    d.MS_20cm_Avg,
                        //    d.EC_20cm_Avg,
                        //    d.TS_20cm_Avg,
                        //    //d.MS_40cm_Avg,
                        //    //d.EC_40cm_Avg,
                        //    //d.TS_40cm_Avg,
                        //    d.MS_60cm_Avg,
                        //    d.EC_60cm_Avg,
                        //    d.TS_60cm_Avg,
                        //    d.MS_80cm_Avg,
                        //    d.EC_80cm_Avg,
                        //    d.TS_80cm_Avg,
                        //    d.MS_100cm_Avg,
                        //    d.EC_100cm_Avg,
                        //    d.TS_100cm_Avg
                        //});
                    }
                    else
                    {
                        data = db.V_TMSHour.Where(d => (d.TIMESTAMP == startDate.Date && d.Hour >= startDate.Hour) || (d.TIMESTAMP == endDate.Date && d.Hour <= endDate.Hour)).Select(d => d).OrderBy(d => new { d.TIMESTAMP, d.Hour });
                    }
                }
    
    
    
                result.data = new { data };
                return result;
            }
    
    
    
    
        public class ApiResult
        {
            public bool success { get; set; }
    
            public string msg { get; set; }
    
            public object data { get; set; }
        }

    T_TMS30min、V_AWSHour、V_TMSHour 是三个不同的实体类

    核心操作是:

     IQueryable data;
     result.data = new { data };
    
    
    树立目标,保持活力,gogogo!
  • 相关阅读:
    周总结13
    周总结11
    《程序员的自我修养》阅读笔记四
    周总结10
    数据导入hive仓库
    周总结9
    《程序员的自我修养》阅读笔记三
    《软件需求》读书笔记四
    《软件需求》读书笔记三
    《软件需求》读书笔记二
  • 原文地址:https://www.cnblogs.com/hao-1234-1234/p/14919913.html
Copyright © 2020-2023  润新知