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

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

被折叠的 条评论
为什么被折叠?



