.NET Core C#系列之XiaoFeng.Data.IQueryableX ORM框架
当前对象操作数据库写法和EF Core极度类似,因为现在大部分程序员都懒得去写SQL,再一个就是项目作大了或其它原因要改数据库,每次改数据库,那么写的SQL语句大部分要作调整,相当麻烦,并且写SQL时字段极容易写错,所以就出现了好多ORM框架,极大的提高了程序员的开发效率。
接下来我们讲一下 XiaoFeng.Data.IQueryableX的用法。
我们在上一节 数据库操作 中有讲过 ConnectionConfig 数据库连接配置对象,不明白的同学可以这节看。本节继续用当前配置对象。
建立数据库连接配置对象,FayElf.Web可为配置名称,也可以为连接串server=.;data=testdata;uid=da;pwd=123; 后边参数为数据库驱动。
var ConnConfig = new ConnectionConfig("FayElf.Web", DbProviderType.SqlServer);
使用 IQueryableX 可以有两种使用方式,下边模型默认就是数据库中的字段名称,也就是它们两个名称要一一对应。如果不一样,则可以用 XiaoFeng.Column 属性去配置数据库中表字段名称,下边分别举例说明。
通过 DataHelperX 创建一个 IQueryableX 对象 然后进行 IQueryableX 运算 去查数据库,IQueryableX和EF Core中的 IQueryable 非常类似,但运行效率比 IQueryable 快很多,并支持多数据库切换。
1.继承了 XiaoFeng.Model.Entity 实体模型的,我们暂且叫A方案,如下边模型
/// <summary>
/// 分类模型
/// </summary>
[Table("Category","分类", "CategoryId",ModelType.Model,"FayElf.Web",0)]
public class Category : Entity<Category>
{
#region 构造器
/// <summary>
/// 无参构造器
/// </summary>
public Category()
{
}
#endregion
#region 属性
/// <summary>
/// 分类名称
/// </summary>
[Column(Name="Names")]
public string Name { get; set; }
/// <summary>
/// 分类说明
/// </summary>
public string Description { get; set; }
/// <summary>
/// 分类ID
/// </summary>
public int CategoryId { get; set; }
/// <summary>
/// 父节点ID
/// </summary>
public int ParentId { get; set; }
/// <summary>
/// 添加日期
/// </summary>
public DateTime AddDate { get; set; }
#endregion
#region 方法
#endregion
}
2.没有继续XiaoFeng.Model.Entity实体模型的,我们暂且叫B方案,如下模型
/// <summary>
/// 用户模型
/// </summary>
public class UserModel
{
#region 构造器
/// <summary>
/// 无参构造器
/// </summary>
public UserModel()
{
}
#endregion
#region 属性
/// <summary>
/// Id
/// </summary>
public long Id { get; set; }
/// <summary>
/// 帐号
/// </summary>
public string Account { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// 性别
/// </summary>
public int Sex { get; set; }
/// <summary>
/// 出生日期
/// </summary>
public DateTime BirthDay { get; set; }
/// <summary>
/// 添加日期
/// </summary>
public DateTime AddDate { get; set; }
#endregion
#region 方法
#endregion
}
首先我们先举实例讲解一下 A方案 的使用方法
//首选实例化一个 Category 实例
var category = Category.Create;
//查询
var category1 = category.Find(a => a.CategoryId == 2);
var category2 = category.Where(a => a.CategoryId == 2).ToEntity();
//上边两种方式是一样的 都是提取 CategoryId 等于 2 的一条数据
//列表
var category3 = category.Where(a => a.CategoryId == 3).ToList();
var category4 = category.ToList(a => a.CategoryId == 3);
//上边两种方式是一样的 都是提取 CategoryId 等于 3 的列表数据
//删除
//当前语句是删除 CateogryId 等于 4 的这条记录
var delete5 = category.Where(a => a.CategoryId == 4).Delete();
//当前语句是删除查从库中查出来的一条数据给删除掉
var delete6 = category1.Delete();
//插入
new Category
{
Name="a", ParentId=0
}.Insert();
//更新
//当前语句是更新字段 为 Name ParentId 的字段值 条件是 CategoryId 等于 5 的记录
var update5 = category.Where(a => a.CategoryId == 5).Update(a => a.Name == "aaa" && a.ParentId == 10);
//当前语句是更新 字段 Name ParentId的值 数据是上边查询出来的一条记录
category1.Name = "bbbb";
category1.ParentId = 3;
var update6 = category.Update();
//联表 下边用法就是 通过 左关联 两张表 on 条件是 第一张表的 ParentId 与第二张表的 CategoryId 相同 然后字段显示为 Name,CategoryId,ParentName 的列表
var join1 = category.QueryableX.Join<Category, object[]>((a, b) => new object[] { JoinType.Left, a.ParentId == b.CategoryId }).Select((a, b) => new
{
a.Name,
a.CategoryId,
ParentName = b.Name
}).ToList();
//更多表关联 有两种方法 第一种是 两张表关联的结果和第三张表关联,再关联的结果与第四经表关联 依次类推
//第二种方法就是 假设一共有5张表关联那么直接实例化 DataHelperX时直接关联5张表,建议用第一种方法。
接着我们讲 B 方案用法
//创建一个带数据库的 IQueryableX 对象,下边所有的操作都是基于当前对象来操作。
var data = new DataHelperX<UserModel>(Conn);
//查询 下边三种写法 运行后 结果是一样的
var userModel1 = data.Where(a => a.Account == "jacky" && a.Password == "admin123").ToEntity();
var userModel2 = data.Where(a => a.Account == "jacky").Where(a => a.Password == "admin123").ToEntity();
var userModel3 = data.First(a => a.Account == "jacky" && a.Password == "admin123");
//列表
var userList1 = data.Where(a => a.Sex == 1 || a.Account == "jacky").ToList();
//关联查询
var userList2 = data.Where(a => a.Sex == 1).Join<UserModel, object[]>((a, b) => new object[] { a.Account == b.Password }).Select((a, b) => new
{
a.Account,
a.Password,
ParentName = b.Account
}).ToList();
//插入
var userModel4 = new UserModel { Account = "jacky", Password = "admin" };
var insert1 = data.Insert(userModel4);
//如果想返回自增长ID则后边输入接收参数即可如下
var insert2 = data.Insert(userModel4, out var ID);
//批量插入
var insert3 = data.Inserts(new List<UserModel> { userModel4,userModel4 });
//更新
var update1 = data.Where(a => a.Id == 1).Update(a => a.Password == "adminadmin");
//删除
var delete1 = data.Where(a => a.Id == 1).Delete();
下边我们说一下 内部函数用法,函数扩展了基本上SQL中用到的函数,以及C#语言中的字符串处理函数的大部分,内部函数扩展方法如下:
/// <summary>
/// 扩展SQL语法
/// Version : 2.0.0
/// Author : jacky
/// Email : jacky@zhuovi.com
/// QQ : 7092734
/// Site : www.zhuovi.com
/// Create Time : 2017/12/20 9:40:00
/// Update Time : 2018/01/19 13:40:00
/// </summary>
public static partial class PrototypeHelper
{
#region 扩展SQL 语法
#region 扩展SQL 字段加值+
/// <summary>
/// 字段加值
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="value">值</param>
/// <returns></returns>
public static T AddSQL<T>(this T obj, T value) { return obj; }
#endregion
#region 扩展SQL 字段减值-
/// <summary>
/// 字段减值
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="value">值</param>
/// <returns></returns>
public static T SubtractSQL<T>(this T obj, T value) { return obj; }
#endregion
#region 扩展SQL 字段乘值*
/// <summary>
/// 字段乘值
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static T MultiplySQL<T>(this T obj, T value) { return obj; }
#endregion
#region 扩展SQL 字段除值/
/// <summary>
/// 字段除值
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static T DivideSQL<T>(this T obj, T value) { return obj; }
#endregion
#region 扩展SQL Between
/// <summary>
/// 扩展SQL Between
/// </summary>
/// <param name="obj">对象</param>
/// <param name="startValue">开始值</param>
/// <param name="endValue">结束值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Boolean BetweenSQL(this object obj, object startValue, object endValue) { return true; }
#endregion
#region 扩展SQL ISNULL
/// <summary>
/// 扩展SQL ISNULL
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static T IsNullSQL<T>(this T obj, T defaultValue) { return defaultValue; }
#endregion
#region 扩展SQL in 语法
/// <summary>
/// 扩展SQL in语法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="array">数组织</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static bool InSQL<T>(this T obj, IEnumerable<T> array) { return true; }
/// <summary>
/// 扩展SQL in语法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="array">数组织</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static bool InSQL<T>(this T obj, T[] array) { return true; }
/// <summary>
/// 扩展SQL in语法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <typeparam name="T1">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="queryableX">IQueryableX对象</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Boolean InSQL<T, T1>(this T obj, IQueryableX<T1> queryableX) { return true; }
/// <summary>
/// 扩展SQL in语法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="array">数组</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static bool InSQL<T>(this T obj, object[] array) { return true; }
/// <summary>
/// 扩展SQL not in语法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="array">数组</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static bool NotInSQL<T>(this T obj, IEnumerable<T> array) { return true; }
/// <summary>
/// 扩展SQL not in语法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="array">数组</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static bool NotInSQL<T>(this T obj, T[] array) { return true; }
/// <summary>
/// 扩展SQL not in语法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <typeparam name="T1">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="queryableX">IQueryableX对象</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Boolean NotInSQL<T, T1>(this T obj, IQueryableX<T1> queryableX) { return true; }
/// <summary>
/// 扩展SQL not in语法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="array">数组</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static bool NotInSQL<T>(this T obj, object[] array) { return true; }
#endregion
#region 扩展SQL like 语法
/// <summary>
/// 扩展SQL like 语法
/// </summary>
/// <param name="str">字符串</param>
/// <param name="likeStr">子串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static bool LikeSQL(this object str, string likeStr) { return true; }
/// <summary>
/// 扩展SQL not like 语法
/// </summary>
/// <param name="str">字符串</param>
/// <param name="likeStr">子串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static bool NotLikeSQL(this string str, string likeStr) { return true; }
/// <summary>
/// 扩展SQL like 语法
/// </summary>
/// <param name="str">字符串</param>
/// <param name="likeStr">子串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static bool LikeSQLX(this object str, string likeStr) { return true; }
/// <summary>
/// 扩展SQL not like 语法
/// </summary>
/// <param name="str">字符串</param>
/// <param name="likeStr">子串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static bool NotLikeSQLX(this string str, string likeStr) { return true; }
#endregion
#region 扩展SQL DateAdd 语法
/// <summary>
/// 扩展SQL DateAdd 语法
/// </summary>
/// <param name="date">日期</param>
/// <param name="val">值</param>
/// <param name="format">格式 年yy,yyyy 季度qq,q 月mm,m 年中的日dy,y 日dd,d 周wk,ww 星期dw,w 小时hh 分钟mi,n 秒ss,s 毫秒ms 微秒mcs 纳秒ns</param>
/// <returns></returns>
public static DateTime DateAddSQL(this DateTime date, long val, string format)
{
return date;
}
/// <summary>
/// 扩展SQL DateAdd 语法
/// </summary>
/// <param name="date">日期</param>
/// <param name="val">值</param>
/// <param name="format">格式 年yy,yyyy 季度qq,q 月mm,m 年中的日dy,y 日dd,d 周wk,ww 星期dw,w 小时hh 分钟mi,n 秒ss,s 毫秒ms 微秒mcs 纳秒ns</param>
/// <returns></returns>
public static DateTime DateAddSQL(this DateTime? date, long val, string format)
{
return date.GetValueOrDefault();
}
#endregion
#region 扩展SQL DateDiff 语法
/// <summary>
/// 扩展SQL DateDiff 语法
/// </summary>
/// <param name="firstDate">第一个时间</param>
/// <param name="SecondDate">第二个时间</param>
/// <param name="format">格式 年yy,yyyy 季度qq,q 月mm,m 年中的日dy,y 日dd,d 周wk,ww 星期dw,w 小时hh 分钟mi,n 秒ss,s 毫秒ms 微秒mcs 纳秒ns</param>
/// <returns></returns>
public static int DateDiffSQL(this DateTime? firstDate, DateTime? SecondDate, string format) { return 1; }
/// <summary>
/// 扩展SQL DateDiff 语法
/// </summary>
/// <param name="firstDate">第一个时间</param>
/// <param name="SecondDate">第二个时间</param>
/// <param name="format">格式 年yy,yyyy 季度qq,q 月mm,m 年中的日dy,y 日dd,d 周wk,ww 星期dw,w 小时hh 分钟mi,n 秒ss,s 毫秒ms 微秒mcs 纳秒ns</param>
/// <returns></returns>
public static int DateDiffSQL(this DateTime firstDate, DateTime SecondDate, string format) { return 1; }
#endregion
#region 扩展SQL DatePart 语法
/// <summary>
/// 扩展SQL DatePart 语法
/// </summary>
/// <param name="Date">当前时间</param>
/// <param name="format">格式 年yy,yyyy 季度qq,q 月mm,m 年中的日dy,y 日dd,d 周wk,ww 星期dw,w 小时hh 分钟mi,n 秒ss,s 毫秒ms 微秒mcs 纳秒ns</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static int DatePartSQL(this DateTime? Date, string format) { return 0; }
/// <summary>
/// 扩展SQL DatePart 语法
/// </summary>
/// <param name="Date">当前时间</param>
/// <param name="format">格式 年yy,yyyy 季度qq,q 月mm,m 年中的日dy,y 日dd,d 周wk,ww 星期dw,w 小时hh 分钟mi,n 秒ss,s 毫秒ms 微秒mcs 纳秒ns</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static int DatePartSQL(this DateTime Date, string format) { return 0; }
#endregion
#region 扩展SQL DateFormat 语法
/// <summary>
/// 扩展SQL DateFormat 语法
/// </summary>
/// <param name="Date">当前时间</param>
/// <param name="format"></param>
/// <returns></returns>
public static string DateFormatSQL(this DateTime? Date, string format) { return string.Empty; }
/// <summary>
/// 扩展SQL DateFormat 语法
/// </summary>
/// <param name="Date">当前时间</param>
/// <param name="format"></param>
/// <returns></returns>
public static string DateFormatSQL(this DateTime Date, string format) { return string.Empty; }
#endregion
#region 扩展SQL Charindex 语法
/// <summary>
/// 扩展SQL Charindex 语法
/// </summary>
/// <param name="str">字符串</param>
/// <param name="IndexStr">子串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static int CharIndexSQL(this string str, string IndexStr) { return 1; }
#endregion
#region 扩展SQL Patindex语法
/// <summary>
/// 扩展SQL Patindex语法
/// </summary>
/// <param name="str">字符串</param>
/// <param name="IndexStr">子串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static int PatindexSQL(this string str, string IndexStr) { return 1; }
#endregion
#region 扩展SQL Abs 算法
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static int AbsSQL(this int value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static int AbsSQL(this int? value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Int16 AbsSQL(this Int16? value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Int16 AbsSQL(this Int16 value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Int64 AbsSQL(this Int64? value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Int64 AbsSQL(this Int64 value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Double AbsSQL(this Double? value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Double AbsSQL(this Double value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static decimal AbsSQL(this decimal? value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static decimal AbsSQL(this decimal value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static float AbsSQL(this float? value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static float AbsSQL(this float value) { return 1; }
#endregion
#region 扩展SQL Floor 算法
/// <summary>
/// 扩展SQL Floor 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Double FloorSQL(this Double? value) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Double FloorSQL(this Double value) { return 1; }
/// <summary>
/// 扩展SQL Floor 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static decimal FloorSQL(this decimal? value) { return 1; }
/// <summary>
/// 扩展SQL Floor 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static decimal FloorSQL(this decimal value) { return 1; }
/// <summary>
/// 扩展SQL Floor 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static float FloorSQL(this float? value) { return 1; }
/// <summary>
/// 扩展SQL Floor 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static float FloorSQL(this float value) { return 1; }
#endregion
#region 扩展SQL Ceiling 算法
/// <summary>
/// 扩展SQL Ceiling 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Double CeilingSQL(this Double? value) { return 1; }
/// <summary>
/// 扩展SQL Ceiling 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Double CeilingSQL(this Double value) { return 1; }
/// <summary>
/// 扩展SQL Ceiling 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static decimal CeilingSQL(this decimal? value) { return 1; }
/// <summary>
/// 扩展SQL Ceiling 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static decimal CeilingSQL(this decimal value) { return 1; }
/// <summary>
/// 扩展SQL Ceiling 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static float CeilingSQL(this float? value) { return 1; }
/// <summary>
/// 扩展SQL Ceiling 算法
/// </summary>
/// <param name="value">值</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static float CeilingSQL(this float value) { return 1; }
#endregion
#region 扩展SQL Round 算法
/// <summary>
/// 扩展SQL Round 算法
/// </summary>
/// <param name="value">值</param>
/// <param name="len">小数点保留长度</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Double RoundSQL(this Double? value, int len) { return 1; }
/// <summary>
/// 扩展SQL Round 算法
/// </summary>
/// <param name="value">值</param>
/// <param name="len">小数点保留长度</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static Double RoundSQL(this Double value, int len) { return 1; }
/// <summary>
/// 扩展SQL Round 算法
/// </summary>
/// <param name="value">值</param>
/// <param name="len">小数点保留长度</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static decimal RoundSQL(this decimal? value, int len) { return 1; }
/// <summary>
/// 扩展SQL Round 算法
/// </summary>
/// <param name="value">值</param>
/// <param name="len">小数点保留长度</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static decimal RoundSQL(this decimal value, int len) { return 1; }
/// <summary>
/// 扩展SQL Abs 算法
/// </summary>
/// <param name="value">值</param>
/// <param name="len">小数点保留长度</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static float RoundSQL(this float? value, int len) { return 1; }
/// <summary>
/// 扩展SQL Round 算法
/// </summary>
/// <param name="value">值</param>
/// <param name="len">小数点保留长度</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static float RoundSQL(this float value, int len) { return 1; }
#endregion
#region 扩展SQL Left
/// <summary>
/// 扩展SQL Left
/// </summary>
/// <param name="str">字符串</param>
/// <param name="num">第几位</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string LeftSQL(this string str, int num) { return ""; }
#endregion
#region 扩展SQL Right
/// <summary>
/// 扩展SQL Right
/// </summary>
/// <param name="str">字符串</param>
/// <param name="num">第几位</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string RightSQL(this string str, int num) { return ""; }
#endregion
#region 扩展SQL Len
/// <summary>
/// 扩展SQL Len
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static int LengthSQL(this string str) { return 1; }
#endregion
#region 扩展SQL Replace
/// <summary>
/// 扩展SQL Replace
/// </summary>
/// <param name="str">字符串</param>
/// <param name="oldString">原字符串</param>
/// <param name="replaceString">新字符串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string ReplaceSQL(this string str, string oldString, string replaceString) { return ""; }
#endregion
#region 扩展SQL Replicate
/// <summary>
/// 扩展SQL Replicate
/// </summary>
/// <param name="str">字符串</param>
/// <param name="num">重复次数</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string ReplicateSQL(this string str, int num) { return ""; }
#endregion
#region 扩展SQL Reverse
/// <summary>
/// 扩展SQL Reverse
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string ReverseSQL(this string str) { return ""; }
#endregion
#region 扩展SQL Stuff
/// <summary>
/// 扩展SQL Stuff
/// </summary>
/// <param name="str">字符串</param>
/// <param name="strat">开始位置</param>
/// <param name="length">长度</param>
/// <param name="replaceString">替换字符串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string StuffSQL(this string str, int strat, int length, string replaceString) { return ""; }
#endregion
#region 扩展SQL Substring
/// <summary>
/// 扩展SQL Substring
/// </summary>
/// <param name="str">字符串</param>
/// <param name="start">开始位置</param>
/// <param name="length">长度</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string SubstringSQL(this string str, int start, int length) { return ""; }
#endregion
#region 扩展SQL Trim
/// <summary>
/// 扩展SQL Ltrim
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string LTrimSQL(this string str) { return ""; }
/// <summary>
/// 扩展SQL Rtrim
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string RTrimSQL(this string str) { return ""; }
/// <summary>
/// 扩展SQL Trim
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string TrimSQL(this string str) { return ""; }
#endregion
#region 扩展SQL Lower
/// <summary>
/// 扩展SQL Lower
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string LowerSQL(this string str) { return ""; }
#endregion
#region 扩展SQL Upper
/// <summary>
/// 扩展SQL Upper
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static string UpperSQL(this string str) { return ""; }
#endregion
#region 扩展SQL Count
/// <summary>
/// 扩展SQL Count
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static int CountSQL(this object str) { return 0; }
#endregion
#region 扩展SQL MAX
/// <summary>
/// 扩展SQL MAX
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">字符串</param>
/// <returns></returns>
public static T MaxSQL<T>(this T obj) { return obj; }
#endregion
#region 扩展SQL MIN
/// <summary>
/// 扩展SQL MIN
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">字符串</param>
/// <returns></returns>
public static T MinSQL<T>(this T obj) { return obj; }
#endregion
#region 扩展SQL SUM
/// <summary>
/// 扩展SQL SUM
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">字符串</param>
/// <returns></returns>
public static T SumSQL<T>(this T obj) { return obj; }
#endregion
#region 扩展SQL AVG
/// <summary>
/// 扩展SQL AVG
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">字符串</param>
/// <returns></returns>
public static T AvgSQL<T>(this T obj) { return obj; }
#endregion
#region 设置字段别名
/// <summary>
/// 设置字段别名
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="ColumnName">别名</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
public static T As<T>(this T obj, string ColumnName) { return obj; }
#endregion
#region 转换类型
/// <summary>
/// 转换类型
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="dataType">目标类型</param>
/// <returns></returns>
public static T CastSQL<T>(this T obj,string dataType) { return obj; }
#endregion
#region SQL参数化调整
/// <summary>
/// SQL参数化调整
/// </summary>
/// <param name="_">SQL语句</param>
/// <param name="dbProvider">数据库驱动</param>
/// <returns></returns>
public static string SQLFormat(this string _, DbProviderType dbProvider)
{
switch (dbProvider)
{
case DbProviderType.Dameng:
return _.ReplacePattern(@"@((Sub_(\d+_)?)?ParamName\d+)", "?");
case DbProviderType.Oracle:
return _.ReplacePattern(@"@((Sub_(\d+_)?)?ParamName\d+)", ":$1");
}
return _;
}
#endregion
#endregion
}
下边举例演示
//内部方法
//查询出分类名称前边包含 字符串 a 的数据
var category5 = category.Where(a => a.Name.StartsWith("a")).ToList();
//查询出分类名第2位开始4个长度的字符是abcd的数据
var category6 = category.Where(a => a.Name.Substring(2, 4) == "abcd").ToList();
//查询 出分类表中添加时间比现在时间少3个时小的 数据
var category7 = category.Where(a => a.AddDate.DateDiffSQL(DateTime.Now, "hh") == 3).ToList();
到这里 IQueryableX 基本使用基本上讲完了,以后还有扩展的 IQueryableQ 它是操作SQL体用的 比如 下边的SQL
if(exists(select top 1 from TestTb where ID = 1))
delete from testTb where ID = 1;
以后会用一节 专一讲 IQueryableQ的使用方法。
开源地址:https://github.com/zhuovi/XiaoFeng
语雀地址:https://www.yuque.com/fayelf/xiaofeng
开源不易,多多支持~