Skip to content

Commit

Permalink
补充查询
Browse files Browse the repository at this point in the history
  • Loading branch information
aierong committed Sep 20, 2023
1 parent e4568bf commit a2da2b0
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions docs/03.NET微服务/02.ORM/03.FreeSql.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,119 @@ var user = fsql.Select<User>().Where(u => u.Name == "张三").FirstOrDefault();


在这个示例中,我们创建了一个名为 User 的类,表示一个用户对象。然后,我们使用 FreeSql 对象来执行基本查询。我们首先查询了所有的用户,然后查询了名为“张三”的用户。



#### 查询where条件

```c#
var blogslist1 = fsql.Select<Blog>()
.Where( b => b.Rating > -3 )
.OrderBy( b => b.Url )
.ToList();

var blogslist2 = fsql.Select<Blog>()
.Where( "Rating>=@min and Rating<=@max",new
{
min=1,
max=10
} )
.OrderBy( b => b.Url )
.ToList();
```

Where可以写多个,实际是多个and条件

```c#
var blogslist1 = fsql.Select<Blog>()
.Where( b => b.Rating > -3 )
.Where ( b=>b.BlogId >50 )
.OrderBy( b => b.Url )
.ToList();
```



#### 查询WhereIf条件

```c#
var blogslist3 = fsql.Select<Blog>()
.Where( "BlogId>=@min and BlogId<=@max" , new
{
min = 1 ,
max = 10
} )
//当条件成立时,才会过滤数据
.WhereIf( 1 == 1, "Rating=@Rating", new
{
Rating=0
} )
.OrderBy( b => b.BlogId )
.ToList();
```



#### 查询(IN查询)

##### in查询

```c#
//数字类型
var list1 = DB.fsql.Select<Blog>()
.Where( a => new[] { 1 , 2 , 3 }.Contains( a.Rating ) )
.ToList();

/*
对应sql:
SELECT a.[BlogId], a.[Url], a.[Rating], a.[BlogTypeId], a.[Times]
FROM[Blog] a
WHERE( ( ( a.[Rating] ) in (1, 2, 3) ))
*/



//字符串类型
var list2 = DB.fsql.Select<Blog>()
.Where( it => it.BlogId >= 1 )
.Where( a => new[] { "piliang_rul" , "rul3" }.Contains( a.Url ) )
.ToList();
/*
对应sql:
SELECT a.[BlogId], a.[Url], a.[Rating], a.[BlogTypeId], a.[Times]
FROM[Blog] a
WHERE( a.[BlogId] >= 1 ) AND( ( ( a.[Url] ) in ( N'piliang_rul' , N'rul3' )))
*/



//使用List 也可以
List<string> barcodelist = new List<string>() { "piliang_rul" , "rul3" };

var list = fsql.Select<SPIBOARDBINDS>()
.Where( it => barcodelist.Contains( it.BARCODE ) )
.ToList();
```

##### in子表查询

```c#
//in子表查询
var list3 = DB.fsql.Select<Blog>()
.Where( it => it.BlogId >= 1 )
.Where( a => DB.fsql.Select<BlogType>().As( "b" ).ToList( b => b.BlogTypeId ).Contains( a.BlogTypeId ) )
.ToList();

/*对应sql:
SELECT a.[BlogId], a.[Url], a.[Rating], a.[BlogTypeId], a.[Times]
FROM [Blog] a
WHERE (a.[BlogId] >= 1) AND (((a.[BlogTypeId]) in (SELECT b.[BlogTypeId]
FROM [BlogType] b)))
*/
```



### 插入数据

```csharp
Expand Down

0 comments on commit a2da2b0

Please sign in to comment.