首页 / C# / 用Linq To SQL 搭建底层
用Linq To SQL 搭建底层
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了用Linq To SQL 搭建底层,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3366字,纯文字阅读大概需要5分钟。
内容图文
![用Linq To SQL 搭建底层](/upload/InfoBanner/zyjiaocheng/519/2650665332434966b018e80f92d48d43.jpg)
用Linq To SQL 搭建底层
接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
namespace Project.DAL
{
interface IBaseService<T> where T:class,new()
{
IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where);
IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, params Expression<Func<T, bool>>[] where);
IQueryable<T> QueryAll<type>(out int total, int skip = 0, int take = 10, Expression<Func<T, type>> order = null, bool isAsc = true, params Expression<Func<T, bool>>[] where);
void Insert(T t);
void Insert(IEnumerable<T> t);
void Delete(T t);
void Delete(IEnumerable<T> t);
bool SaveChange();
}
}
底层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
namespace Project.DAL
{
public class BaseService<T> : IBaseService<T> where T : class, new()
{
//上下文对象
Models.QuestionDBDataContext dbContext = new Models.QuestionDBDataContext();//直接复制会报错应先在Models里引入数据库
private IQueryable<T> table = null;
private IQueryable<T> GetTable()
{
if (table == null)
{
table = (IQueryable<T>)dbContext.GetTable<T>();
}
return table;
}
/// <summary>
/// 查询
/// </summary>
/// <param name="where">查询条件</param>
/// <returns></returns>
public IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where)
{
IQueryable<T> iq = GetTable();
if (where != null || where.Length < 1)
{
foreach (var item in where)
{
iq = iq.Where(item);
}
}
return iq;
}
/// <summary>
/// 排序
/// </summary>
/// <typeparam name="type">排序类型</typeparam>
/// <param name="order">排序列</param>
/// <param name="isAsc">升序or降序</param>
/// <param name="where"></param>
/// <returns></returns>
public IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, params Expression<Func<T, bool>>[] where)
{
var iq = QueryAll(where);
if (isAsc)
{
iq = iq.OrderBy(order);
}
else
{
iq = iq.OrderByDescending(order);
}
return iq;
}
/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="type"></typeparam>
/// <param name="total">总数据数</param>
/// <param name="skip">跳过n条</param>
/// <param name="take">取n条(一页显示几条)</param>
/// <param name="order"></param>
/// <param name="isAsc"></param>
/// <param name="where"></param>
/// <returns></returns>
public IQueryable<T> QueryAll<type>(out int total, int skip = 0, int take = 10, Expression<Func<T, type>> order = null, bool isAsc = true, params Expression<Func<T, bool>>[] where)
{
var iq = QueryAll<type>(order, isAsc, where);
total = iq.Count();
return iq.Skip(skip).Take(take);
}
public void Insert(T t)
{
dbContext.GetTable<T>().InsertOnSubmit(t);
}
public void Insert(IEnumerable<T> t)
{
dbContext.GetTable<T>().InsertAllOnSubmit(t);
}
public void Delete(T t)
{
dbContext.GetTable<T>().DeleteOnSubmit(t);
}
public void Delete(IEnumerable<T> t)
{
dbContext.GetTable<T>().DeleteAllOnSubmit(t);
}
/// <summary>
/// 保存修改
/// </summary>
/// <returns></returns>
public bool SaveChange()
{
try
{
dbContext.SubmitChanges();
return true;
}
catch (Exception e)
{
return false;
}
}
}
}
用Linq To SQL 搭建底层
标签:get except namespace ext context code reading 查询条件 private
本文系统来源:https://www.cnblogs.com/setsuna-cn/p/12238422.html
内容总结
以上是互联网集市为您收集整理的用Linq To SQL 搭建底层全部内容,希望文章能够帮你解决用Linq To SQL 搭建底层所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。