mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-04 23:40:53 +08:00
Updated Query from Multi Tables (markdown)
@@ -78,47 +78,21 @@ fsql.Select<Topic, Category, CategoryType>()
|
||||
|
||||
> Experience: [One-to-many, only the last record is taken from the associated table ](https://github.com/dotnetcore/FreeSql/issues/430)
|
||||
|
||||
## 3. WithSql
|
||||
3、WithoutJoin
|
||||
fsql.Select<Order, Product, User>()
|
||||
.InnerJoin((o, p, u) => o.UserId == u.Id)
|
||||
.LeftJoin((o, p, u) => o.ProductId == p.Id)
|
||||
.WithoutJoin(t2: !includeProductInfo)
|
||||
.ToList((o, p, u) => new OrderDetailDto
|
||||
{
|
||||
OrderId = o.Id,
|
||||
OrderNo = o.OrderNo,
|
||||
Username = u.Username, // User (T3) 始终 JOIN,可直接引用
|
||||
ProductName = includeProductInfo ? p.Name : "N/A"
|
||||
});
|
||||
|
||||
```csharp
|
||||
fsql.Select<Topic, Category, CategoryType>()
|
||||
.WithSql(
|
||||
"select * from Topic where id=@id1",
|
||||
"select * from Category where id=@id2",
|
||||
null, //Do not set the SQL corresponding to CategoryType
|
||||
new { id1 = 10, id2 = 11, id3 = 13 }
|
||||
)
|
||||
.LeftJoin((a,b,c) => a.CategoryId == b.Id)
|
||||
.LeftJoin((a,b,c) => b.ParentId == c.Id)
|
||||
.ToList();
|
||||
|
||||
//SELECT ...
|
||||
//FROM ( select * from Topic where id=@id1 ) a
|
||||
//LEFT JOIN ( select * from Category where id=@id2 ) b ON a.`CategoryId` = b.`Id`
|
||||
//LEFT JOIN `CategoryType` c ON b.`ParentId` = c.`Id`
|
||||
```
|
||||
|
||||
> Tip: `ISelect.ToSql` can be used with `WithSql`
|
||||
|
||||
> v3.2.666 [UnionAll Query](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)、[WithTempQuery + FromQuery Nested Query](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2)
|
||||
|
||||
> v3.2.666 WithMemory Query using memory data
|
||||
|
||||
```csharp
|
||||
var list = new List<Topic>();
|
||||
list.Add(new Topic { ... });
|
||||
list.Add(new Topic { ... });
|
||||
|
||||
fsql.Select<Topic>()
|
||||
.WithMemory(list)
|
||||
.ToList()
|
||||
//SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime`
|
||||
//FROM (
|
||||
// SELECT ...
|
||||
// UNION ALL
|
||||
// SELECT ...
|
||||
//) a
|
||||
```
|
||||
// includeProductInfo = true: SQL JOIN Product
|
||||
// includeProductInfo = false: SQL will not JOIN Product, ProductName will be "N/A"
|
||||
|
||||
## 4. SQL join table
|
||||
|
||||
|
||||
Reference in New Issue
Block a user