赞
踩
using SqlSugar;
using Sugar.Enties;
using SugarContext;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace SugarCore
{
class Program
{
static SugarDbContext sugar = new SugarDbContext();
static void Main(string[] args)
{
var db = sugar.Db;
var queryable = db.Queryable<Student>();
//拼接会比EF方便些,不像EF需要queryable+=
queryable.Where(it => it.StudentName.Contains("陈"));
queryable.Where(it => it.StudentSex == "女");
//防止queryable相互影响我们用clone解决
var StudentName = queryable.Clone().Select(it => it.StudentName).First();
/正确答案是两条数据 如果去掉Clone,受上面一条影响,只会有一条数据
var list = queryable.Clone().ToList();//正确答案是两条数据 如果去掉Clone,受上面一条影响,只会有一条数据
///案例1: WhereIF函数
///根据条件判段是否执行过滤,我们可以用WhereIf来实现,true执行过滤,false则不执行
///
var a = "陈";
var b = "飞";
var c = "女";
///陈性女同学一个
var list2 = db.Queryable<Student>()
.WhereIF(!string.IsNullOrEmpty(a), it => it.StudentName.StartsWith(a))
.WhereIF(!string.IsNullOrEmpty(b), it => it.StudentName.EndsWith(b))
.WhereIF(!string.IsNullOrEmpty(c), it => it.StudentSex == c).ToList();
//
///所有叫陈飞的童鞋9人 string.IsNullOrEmpty(c) 这个语句不会执行
var list3 = db.Queryable<Student>()
.WhereIF(!string.IsNullOrEmpty(a), it => it.StudentName.StartsWith(a))
.WhereIF(!string.IsNullOrEmpty(b), it => it.StudentName.EndsWith(b))
.WhereIF(string.IsNullOrEmpty(c), it => it.StudentName == c).ToList();
///
/*
案例2.:MergeTable 函数 4.4
是将多表查询的结果Select里的内容变成一张表, 如果是多表查询的时候,我们无论是使用 where 还是 orderBy 都需要加别名,这样我们就不能实现动态排序,因为我不知道别名叫什么, 可以用MergeTable解决这个问题
*/
///多表查询方式
var pageJoin = db.Queryable<Student, StudentGrad>((st, sc) => new object[]
{
JoinType.Inner, st.GradID == sc.GradID
})
.Where(st => st.StudentName.EndsWith("芬"))//别名是st
.OrderBy("st.StudentName asc")//别名是sc
.Select((st, sc) => new { StudentName = st.StudentName, gradeName = sc.GradName })
.ToList();
///等同于MergeTable 方式
///
var pageJoin_2 = db.Queryable<Student, StudentGrad>((st, sc) => new object[]
{
JoinType.Inner,st.GradID==sc.GradID
})
.Select((st, sc) => new
{
StudentName = st.StudentName,
gradeName = sc.GradName
})
.MergeTable()
.Where(A => A.StudentName.EndsWith("芬")).OrderBy("StudentName asc").ToList();//别名不限
///案例3: SqlQueryable 4.5.2.5 , 可以方便的把SQL变成表来操作 直接执行SQL语句
///
var t12 = db.SqlQueryable<Student>("select * from student").ToPageList(1, 2);
///案例4: 将表单组装成 List<ConditionalModel>实现查询 4.5.9
///查询女生中 带有 飞 子的同学
List<IConditionalModel> conModels = new List<IConditionalModel>();
conModels.Add(new ConditionalModel() { FieldName = "StudentSex", ConditionalType = ConditionalType.Equal, FieldValue = "女" });
conModels.Add(new ConditionalModel() { FieldName = "StudentName", ConditionalType = ConditionalType.Like, FieldValue = "飞" });
var student = db.Queryable<Student>().Where(conModels).ToList();
///
//4.6.4.4 版本支持了 复杂的OR
// and StudentSex='女' And (StudentName='陈芬' or StudentName='王芬' Or StudentName='李芬')
List<IConditionalModel> conModels__22 = new List<IConditionalModel>();
conModels__22.Add(new ConditionalModel() { FieldName = "StudentSex", ConditionalType = ConditionalType.Equal, FieldValue = "女" });
conModels__22.Add(new ConditionalCollections()
{
ConditionalList =
new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()
{
new KeyValuePair<WhereType, ConditionalModel>
( WhereType.And , //And
new ConditionalModel() { FieldName = "StudentName", ConditionalType = ConditionalType.Equal, FieldValue = "陈芬" }),
new KeyValuePair<WhereType, ConditionalModel>
(WhereType.Or,
new ConditionalModel() { FieldName = "StudentName", ConditionalType = ConditionalType.Equal, FieldValue = "王芬" }),
new KeyValuePair<WhereType, ConditionalModel>
( WhereType.Or,
new ConditionalModel() { FieldName = "StudentName", ConditionalType = ConditionalType.Equal, FieldValue = "李芬" })
}
});
var studentResult = db.Queryable<Student>().Where(conModels__22).ToList();
///案例5: 拼接拉姆达 4.5.9.8
///
var exp = Expressionable.Create<Student>()
.OrIF(1 == 1, it => it.StudentSex == "女")
.And(it => it.StudentName.Contains("陈"))
.AndIF(2 == 3, it => SqlFunc.IsNullOrEmpty(it.StudentName)) //此where 不执行
.Or(it => it.StudentName.Contains("飞")).ToExpression();//拼接表达式
var list55 = db.Queryable<Student>().Where(exp).ToList();
///Queryable是支持字符串与拉姆达混用或者纯字符串拼接模式,可以满足复杂的一些需求
///复杂动态 表达式和SQL子查询混合模式
///
例子1:
var queryable = db.Queryable<Student>("t");
queryable.Where("t.id in (select id from xxx)");
queryable.Where(it => it.Id == 1);
//更多操作拼接qureyable
var result = queryable.Select(@"
id,
name,
(select name form school where shoolid=t.id) as schoolName
").ToList();
例子2:
dynamic join3 = db.Queryable("Student", "st")
.AddJoinInfo("School", "sh", "sh.id=st.schoolid")
.Where("st.id>@id")
.AddParameters(new { id = 1 })
.Select("st.*").ToList(); //也可以Select<T>(“*”).ToList()返回实体集合
例子3:
var list = db.Queryable<Student>().
Select(it => new Student()
{
Name = it.Name,
Id = SqlFunc.MappingColumn(it.Id, "(select top 1 id from school)") // 动态子查询
}).ToList();
///安全拼SQL
///安全拼SQL 安全拼SQL 安全拼SQL 安全拼SQL 安全拼SQL 安全拼SQL 安全拼SQL 安全拼SQL 安全拼SQL
安全拼SQL
使用参数化过滤
private static void Where()
{
var db = GetInstance();
string value = "'jack';drop table Student";
var list = db.Queryable<Student>().Where("name=@name", new { name = value }).ToList();
//没有发生任何事情
}
字段是无法用参数化实现的,我们就可以采用这种方式过滤
private static void OrderBy()
{
var db = GetInstance();
try
{
var propertyName = "Id'"; //类中的属性的名称
var dbColumnName = db.EntityProvider.GetDbColumnName<Student>(propertyName);
var list2 = db.Queryable<Student>().OrderBy(dbColumnName).ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.ReadLine();
}
}
class ViewModel
{
public string StudentName { get; set; }
public string GradName { get; set; }
}
class StuModel
{
public string StudentName { get; set; }
public string StuSex { get; set; }
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。