使用EntityFramework和Sqlsugar 查询最近插入的几条数据

本文介绍了如何在上位机中通过Entity Framework和Sugar ORM快速获取数据库中最新两条实际数据,展示了排序和分页操作的简化实现。

在上位机中, 经常需要查询最近的几条数据. 但是查询数据库 默认为从最早的数据查询, 这时候仅仅需要将查询的表降序排列即可

Model

namespace Test
{
   
   
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("ActualData")]
    public partial class ActualData
    {
   
   
        public int Id {
   
    get; set; }

        public DateTime? InsertTime {
   
    get; set; }

        [StringLength(20)]
        public string VarName {
   
    get; set; }

        [StringLength(20)]
        public string Value {
   
    get; set; }

        [StringLength(50)]
        public string Remark {
   
    get; set; }
    }
}

用EntityFramework

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test.Provider
{
   
   
    interface IProvider
    {
   
   
        List<ActualData> GetActualDatas();
        
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test.Provider
{
   
   
    public class ModelsResponsitory : IProvider
    {
   
   

        private Entities _entities = new Entities();


        public List<ActualData> GetActualDatas()
        {
   
   
            return _entities.ActualData.OrderByDescending(i => i.Id).Take(2).ToList();
            
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.Provider;
using Test.Sugar;

namespace Test
{
   
   
    class Program
    {
   
   
        static void Main(string[] args)
        {
   
   
            var entities = new ModelsResponsitory();
            List<ActualData> actualDatas = entities.GetActualDatas();

            //EntityFramework
            foreach (var item in actualDatas)
            {
   
   
                Console.WriteLine(item.Value);
            }

            //Sugar
            var sugarEntities = new SugarResponsitory();
            List<ActualData> sugaractualDatas = sugarEntities.GetActualDatas();
            foreach (var item in sugaractualDatas)
            {
   
   
                Console.WriteLine(item.Value);
            }

            Console.ReadKey();
        }
    }
}

使用 Sqlsugar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test.Sugar
{
   
   
    public interface IProviderSugar
    {
   
   
        List<ActualData> GetActualDatas();
    }
}

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace Test.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值