Skip to content
FreeSql DocumentsFreeSql Documents
Guide
NuGetopen in new window
Apiopen in new window
github icon
  • Guide

      • Query Data
        • Pagination
          • Query from Single Table
            • Single Table
              • WithSql
                • Reference
                • Query from Multi Tables
                  • Group Aggregation Query
                    • Return Data
                      • Lazy Loading
                        • Greed-Loading
                          • Linq to Sql
                            • withsql
                              • Parent Child Relationship Query

                            Query from Single Table

                            author iconnicyecalendar iconNovember 21, 2021timer iconLess than 1 minuteword iconAbout 261 words

                            On This Page
                            • Single Table
                            • WithSql
                            • Reference

                            # Query from Single Table

                            static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
                                .UseConnectionString(FreeSql.DataType.MySql, connectionString)
                                .Build(); //Be sure to define as singleton mode
                            
                            class Topic {
                                [Column(IsIdentity = true)]
                                public int Id { get; set; }
                                public string Title { get; set; }
                                public int Clicks { get; set; }
                                public DateTime CreateTime { get; set; }
                            
                                public int CategoryId { get; set; }
                            }
                            
                            1
                            2
                            3
                            4
                            5
                            6
                            7
                            8
                            9
                            10
                            11
                            12
                            13

                            # Single Table

                            fsql.Select<Topic>()
                              .Where(a => a.Id == 10)
                              .ToList();
                            ///SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime` 
                            //FROM `Topic` a 
                            //WHERE (a.`Id` = 10)
                            
                            fsql.Select<Topic>()
                              .Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100)
                              .ToList();
                            ///SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime` 
                            //FROM `Topic` a 
                            //WHERE (a.`Id` = 10 AND a.`Id` > 10 OR a.`Clicks` > 100)
                            
                            fsql.Select<Topic>()
                              .Where(a => new []{1,2,3}.Contains(a.Id))
                              .ToList();
                            //SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime` 
                            //FROM `Topic` a 
                            //WHERE (a.`Id` in (1,2,3))
                            
                            1
                            2
                            3
                            4
                            5
                            6
                            7
                            8
                            9
                            10
                            11
                            12
                            13
                            14
                            15
                            16
                            17
                            18
                            19
                            20

                            # WithSql

                            fsql.Select<Topic>()
                              .WithSql("select * from Topic where clicks > @val", new { val = 10 })
                              .Page(1, 10)
                              .ToList()
                            //SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime` 
                            //FROM (select * from Topic where clicks > @val) a 
                            
                            1
                            2
                            3
                            4
                            5
                            6

                            When WithSql is used multiple times, it will be converted to UNION ALL query

                            # Reference

                            • 《Query from Multi Tables》
                            • 《Return Data》
                            • 《LinqToSql》
                            • 《Repository Layer》
                            • 《FreeSql Optimization: Lazy Loading》
                            • 《FreeSql Optimization: Greed Loading》
                            • 《Expression Function》
                            • 《Performance》
                            • 《Sharding Tables and Database》
                            • 《Tenant》
                            edit iconEdit this pageopen in new window
                            Last update: 11/21/2021, 4:13:16 PM
                            Contributors: luoyunchong
                            Prev
                            Pagination
                            Next
                            Query from Multi Tables
                            Copyright © 2018-present nicye
                            Copyright © 2022 nicye

                            This app can be installed on your PC or mobile device. This will allow this web app to look and behave like any other installed app. You will find it in your app lists and be able to pin it to your home screen, start menus or task bars. This installed web app will also be able to safely interact with other apps and your operating system.

                            Description