1. 程式人生 > >lambda表達式封裝對數據庫的查詢

lambda表達式封裝對數據庫的查詢

text list() int attr font 優雅 ima row not

前言:

1.為什麽要封裝lambda表達式數據庫查詢,原因有一下幾點:

1.1.在以往的開發中進行數據庫表查詢時,其實所需要的字段就是其中幾個,但是在開發中,開發者往往習慣select * 進行查詢,當數據多和用戶量多時,查詢的效率會降低。

1.2.在寫查詢where條件的時候,總是用string.format去拼接字符串,開發效率低。

1.3.代碼不夠優雅,代碼中嵌套和多sql語句,如果是表字段發生改變時編譯器檢查不出來,代碼出錯的概率大。

1.4.本著 write less do more 原則

2.代碼展示和類結構:

技術分享

文件目錄:

技術分享

分析:

設計的思路是模仿數據庫的查詢語句,如: select a,b..... from tab where a=‘’ Order by a 基本結構寫,其中help是解析lambda表達式,獲取到對應字段的名稱。

opreationclass中看文件的名稱就可以很好的區分,DbSelect 中可以獲取到要查詢的字段和Operationwhere的對象並把查詢字段值賦值給operationwhere屬性字段,

operationwhere 可以獲取大查詢的條件字符串,在把查詢字段和查詢條件字符串傳遞給oprationExcute 進行查詢返回結果集(也可以傳給oprationOderby,在傳遞給oprationExcute ),oprationExcute是執行返回數據集結果,在把結果集傳給TransformationData進行list和datatable,json的轉換。

源代碼:

1.GetClomun:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace GTJ.Core.Select.Helpers
{
public class GetClomun
{
/// <summary>
/// [Display(Name = "")]
/// 獲得類屬性中標記的名稱
/// </summary>
/// <param name="expr"></param>
/// <returns></returns>
public static string GetDisplayName(Expression expr)
{
var memberParam = expr as MemberExpression;
if (memberParam != null)
{
return GetDisplayName(memberParam);
}
var unary = expr as UnaryExpression;
if (unary != null)
{
return GetDisplayName(unary.Operand as MemberExpression);
}
var call = expr as MethodCallExpression;
if (call != null)
{
return GetDisplayName(call.Object as MemberExpression);
}

return string.Empty;

}

/// <summary>
/// [Display(Name = "記住帳號")]
/// 獲得類屬性中標記的中文名
/// </summary>
/// <param name="memberParam"></param>
/// <returns></returns>
private static string GetDisplayName(MemberExpression memberParam)
{
var name = memberParam.Member.Name;
var property = memberParam.Member.ReflectedType.GetProperty(name);
var displays = property.GetCustomAttributes(typeof(DisplayAttribute), false);
if (displays == null || displays.Length == 0)
return property.Name;
else
return (displays[0] as DisplayAttribute).Name;
}
}
}

2.OprationExcute

using CYQ.Data;
using CYQ.Data.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GTJ.Core.Select.OperationClass
{
public class OprationExcute<T> where T:new()
{
/// <summary>
/// 要顯示的列
/// </summary>
public List<string> column = new List<string>();
/// <summary>
/// 查詢字符串
/// </summary>
public string WhereStr = null;
/// <summary>
/// 排序字符串
/// </summary>
public string OderByStr = null;
/// <summary>
/// 執行顯示的條數
/// </summary>
/// <param name="top"></param>
/// <returns></returns>
public TransformationData<T> Excute(string top)
{
if (this.OderByStr == null)
{
this.OderByStr = "CreateTime desc";
}
TransformationData<T> Data = new TransformationData<T>();
Data.Table= GetMDataTable(this.column.ToArray(), top, this.WhereStr+ " order by " + this.OderByStr,null);
return Data;
}

private MDataTable GetMDataTable(string[] arr = null, string top = null, string where = null, string name = null)
{
if (name == null)
{
name = new T().ToString();
}
using (MAction action = new MAction(name))
{
if (arr != null)
{
var columnStr = "";
for (var i = 0; i < arr.Length; i++)
{
columnStr += arr[i].ToString();
columnStr += ",";
}
columnStr = columnStr.TrimEnd(‘,‘);
action.SetSelectColumns(columnStr);
}
if (where != null && top != null)
{
return action.Select(Convert.ToInt32(top), where);

}
else if (where == null && top != null)
{
return action.Select(Convert.ToInt32(top));
}
else if (where != null && top == null)
{
return action.Select(where);
}
else
{
return action.Select();
}
}
}
}
}

3.OprationFindModel

using CYQ.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace GTJ.Core.Select.OperationClass
{
public class OprationFindModel<T> where T:new()
{
/// <summary>
/// 查詢model情況
/// </summary>
/// <param name="funcs"></param>
public T FindModel(params Expression<Func<T, bool>>[] funcs)
{
GTJ.Core.AiExpConditions<T> exp = new AiExpConditions<T>();

for(var i = 0; i < funcs.Length; i++)
{
exp.AddAndWhere(funcs[i]);
}

return GetModelData(exp.Where(false));
}

private T GetModelData(string where, string name = null)
{
try
{
if (name == null)
{
name = new T().ToString();
}
using (MAction action = new MAction(name))
{
if (action.Fill(where))
{
return action.Data.ToEntity<T>();
}
else
{
return new T();
}
}

}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
}
}

4.OprationOderByDesc

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

namespace GTJ.Core.Select.OperationClass
{
public class OprationOderByDesc<T> where T:new ()
{
/// <summary>
/// 要顯示的列
/// </summary>
public List<string> column = new List<string>();
/// <summary>
/// 查詢字符串
/// </summary>
public string WhereStr = null;
public OprationExcute<T> OderByDesc(bool result,params Expression<Func<T,string>>[] funcs)
{
List<string> OderBy = new List<string>();
OprationExcute<T> excute = new OprationExcute<T>();
for (var i = 0; i < funcs.Length; i++)
{
OderBy.Add(Helpers.GetClomun.GetDisplayName(funcs[i].Body));
}
excute.OderByStr = string.Join(",", OderBy.ToArray());
if (result == true)
{
excute.OderByStr += " desc";
}
else
{
excute.OderByStr += " asc";
}
excute.column = this.column;
excute.WhereStr = this.WhereStr;
return excute;
}
}
}

5.OprationWhere

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

namespace GTJ.Core.Select.OperationClass
{
public class OprationWhere<T> where T:new()
{
/// <summary>
/// 要顯示的列
/// </summary>
public List<string> column = new List<string>();

/// <summary>
/// 查詢條件
/// </summary>
/// <param name="funcs"></param>
/// <returns></returns>
public OprationExcute<T> Where(params Expression<Func<T, bool>>[] funcs)
{
OprationExcute<T> Excute = new OprationExcute<T>();
GTJ.Core.AiExpConditions<T> exp = new AiExpConditions<T>();
for(var i = 0; i < funcs.Length; i++)
{
exp.AddAndWhere(funcs[i]);
}
Excute.WhereStr = exp.Where(false);
Excute.column = this.column;
return Excute;
}

public OprationOderByDesc<T> Where(bool resutl ,params Expression<Func<T, bool>>[] funcs)
{
OprationOderByDesc<T> OderBy = new OprationOderByDesc<T>();
GTJ.Core.AiExpConditions<T> exp = new AiExpConditions<T>();
for (var i = 0; i < funcs.Length; i++)
{
exp.AddAndWhere(funcs[i]);
}
OderBy.WhereStr = exp.Where(false);
OderBy.column = this.column;

return OderBy;
}
}
}

6.TransformationData

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CYQ.Data.Table;
using CYQ.Data;

namespace GTJ.Core.Select.OperationClass
{
public class TransformationData<T> where T : new ()
{

public MDataTable Table = new MDataTable();
/// <summary>
/// 轉成list
/// </summary>
public List<T> ToList()
{
return Table.ToList<T>();
}

public DataTable ToDataTable()
{
return Table.ToDataTable();
}

public string ToJson()
{
return Table.ToJson();
}
}
}

7.DbSelect

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

/// <summary>
/// zouhp 2017.6.16 V1
/// lambda 快速進行單表查詢
/// </summary>
namespace GTJ.Core.Select
{
/// <summary>
/// lambda快速查詢
/// </summary>
/// <typeparam name="T"></typeparam>
public class DbSelect<T> where T:new()
{
public OperationClass.OprationWhere<T> Select(params Expression<Func<T,string>>[] funcs)
{
OperationClass.OprationWhere<T> where = new OperationClass.OprationWhere<T>();
for (var i = 0; i < funcs.Length; i++)
{
where.column.Add(Helpers.GetClomun.GetDisplayName(funcs[i].Body));
}
return where;
}

public T Select(params Expression<Func<T, bool>>[] funcs)
{
OperationClass.OprationFindModel<T> Model = new OperationClass.OprationFindModel<T>();
return Model.FindModel(funcs);
}
}
}

lambda表達式封裝對數據庫的查詢