FreeSql (二十四)Linq To Sql 语法使用介绍

原本不支持 IQueryable 主要出于使用习惯的考虑,如果继承 IQueryable,编写代码的智能总会提示出现一堆你不想使用的方法(对不起,我有强迫症),IQueryable 自身提供了一堆没法实现的方法,还有外部入侵的扩展方法,严重影响编码体验。如下图:

image

原以为必须实现 IQueryable 才可以实现,结果一次惊喜,原来只要有对应的方法就成。

虽然支持了,但是还是推荐使用【链式 + lambda】 !!!

特别说明

这次功能更新,ISelect 增加了 5个方法,对【链式 + lambda】的用户可能会造成少许影响,我在注释上标明了,如下图:

image

特别是 .Select(),原先没有支持,该功能与 ToList(a => new Dto{}) 合并实现的。

需要避免一下坑:

  • 如果一定要使用 .Select() 方法,请务必在 .ToList() 之前调用它;

  • 请减少图中方法在【链式 + labmda】模式下的使用;

所有 ISelect 都可以使用 linq to sql,包括 Repository、DbContext;

Where

var t1 = (
    from a in fsql.Select<Student>()
    where a.id == item.id
    select a
).ToList();

Select(指定字段)

var t1 = (
    from a in fsql.Select<Student>()
    where a.id == item.id
    select new { a.id }
).ToList();

CaseWhen

var t1 = (
    from a in fsql.Select<Student>()
    where a.id == item.id
    select new {
        a.id,
        a.name,
        testsub = new {
            time = a.age > 10 ? "大于" : "小于或等于"
        }
    }
).ToList();

Join

var t1 = (
    from a in fsql.Select<Student>()
    join b in fsql.Select<School>() on a.id equals b.StudentId
    select a
).ToList();

var t2 = (
    from a in fsql.Select<Student>()
    join b in fsql.Select<School>() on a.id equals b.StudentId
    select new { a.id, bid = b.id }
).ToList();

var t3 = (
    from a in fsql.Select<Student>()
    join b in fsql.Select<School>() on a.id equals b.StudentId
    where a.id == item.id
    select new { a.id, bid = b.id }
).ToList();

LeftJoin

var t1 = (
    from a in fsql.Select<Student>()
    join b in fsql.Select<School>() on a.id equals b.StudentId into temp
    from tc in temp.DefaultIfEmpty()
    select a
).ToList();

var t2 = (
    from a in fsql.Select<Student>()
    join b in fsql.Select<School>() on a.id equals b.StudentId into temp
    from tc in temp.DefaultIfEmpty()
    select new { a.id, bid = tc.id }
).ToList();

var t3 = (
    from a in fsql.Select<Student>()
    join b in fsql.Select<School>() on a.id equals b.StudentId into temp
    from tc in temp.DefaultIfEmpty()
    where a.id == item.id
    select new { a.id, bid = tc.id }
).ToList();

From(多表查询)

var t1 = (
    from a in fsql.Select<Student>()
    from b in fsql.Select<School>()
    where a.id == b.StudentId
    select a
).ToList();

var t2 = (
    from a in fsql.Select<Student>()
    from b in fsql.Select<School>()
    where a.id == b.StudentId
    select new { a.id, bid = b.id }
).ToList();

var t3 = (
    from a in fsql.Select<Student>()
    from b in fsql.Select<School>()
    where a.id == b.StudentId
    where a.id == item.id
    select new { a.id, bid = b.id }
).ToList();

GroupBy(分组)

var t1 = (
    from a in fsql.Select<Student>()
    where a.id == item.id
    group a by new {a.id, a.name } into g
    select new {
        g.Key.id, g.Key.name,
        cou = g.Count(),
        avg = g.Avg(g.Value.age),
        sum = g.Sum(g.Value.age),
        max = g.Max(g.Value.age),
        min = g.Min(g.Value.age)
    }
).ToList();

系列文章导航

转载于:https://www.cnblogs.com/FreeSql/p/11531392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FreeSql是一种高性能免费开源的ORM框架,可以用于在C#中操作关系型数据库。以下是在C#中使用FreeSql的步骤: 1. 安装FreeSql NuGet包 在Visual Studio中打开NuGet包管理器,搜索FreeSql并安装最新版。 2. 配置数据库连接 在App.config或Web.config文件中添加数据库连接字符串,例如: ```xml <connectionStrings> <add name="MySql" connectionString="Server=localhost;Database=mydatabase;User=root;Password=123456;"/> </connectionStrings> ``` 3. 初始化FreeSql 在应用程序启动时,初始化FreeSql并注册数据提供程序,例如: ```csharp // 初始化FreeSql var connectionString = ConfigurationManager.ConnectionStrings["MySql"].ConnectionString; var fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.MySql, connectionString) .UseAutoSyncStructure(true) .Build(); // 注册数据提供程序 FreeSql.Provider.Register<CustomMySqlProvider>(FreeSql.DataType.MySql); ``` 4. 编写数据访问代码 使用FreeSql的实体类、仓储类和LINQ语句,编写数据访问代码,例如: ```csharp // 定义实体类 public class User { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } // 定义仓储类 public class UserRepository : FreeSqlRepository<User> { public UserRepository() : base() { } public UserRepository(UnitOfWorkManager uowm) : base(uowm) { } } // 使用LINQ语句查询数据 var users = fsql.Select<User>().Where(u => u.Age > 18).ToList(); // 使用仓储类查询数据 var userRepo = new UserRepository(); var user = userRepo.Where(u => u.Name == "Tom").FirstOrDefault(); // 使用事务提交数据 using (var uow = fsql.CreateUnitOfWork()) { var userRepo = uow.GetRepository<User>(); userRepo.Insert(new User { Name = "Jack", Age = 20 }); userRepo.Update(new User { Id = 1, Name = "Tom", Age = 22 }); uow.Commit(); } ``` 以上就是在C#中使用FreeSql的基本步骤,可以根据实际情况进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值