Multi-table data taken FreeSql
Articles and essays to classify as an example.
- Table structure
part as a field, the other is omitted in order to display many association, the classification may be a plurality of articles. A paper belongs to a classification.
blog_article (Essay table)
Field | Types of | Remark |
---|---|---|
id | int | |
classify_id | int | Category id |
title | varchar(50) | title |
content | varchar(4000) | text |
blog_classify (Essay Category column)
Field | Types of | Remark |
---|---|---|
id | int | |
ClassifyName | varchar(50) | Category name |
Which FullAduitEntity, Entity, in open source projects , you can search on their own, which is to create time in ABP, such as whether to delete fields
Article.cs
[Table(Name = "blog_article")]
public class Article : FullAduitEntity
{
/// <summary>
/// 文章所在分类专栏Id
/// </summary>
public int? ClassifyId { get; set; }
public Classify Classify { get; set; }
/// <summary>
/// 标题
/// </summary>
[Column(DbType = "varchar(200)")]
public string Title { get; set; }
/// <summary>
/// 正文
/// </summary>
[Column(DbType = "text")]
public string Content { get; set; }
}
Classify.cs
[Table(Name = "blog_classify")]
public class Classify:FullAduitEntity
{
public string ClassifyName { get; set; }
public List<Article> Articles { get; set; }
}
Using conventional navigation attributes
- Classify property is null
List<Article> articles1 = _articleRepository
.Select
.ToList();
2. Classify property value will
we get the data in the foreground, you can also take Classify cycle in direct property
List<Article>articles2= _articleRepository
.Select
.Include(r => r.Classify)
.ToList();
If, back there in order to filter out some of the fields can be used AutoMapper, passed to the front desk to use Dto, filtered creation time, modification time audit log
Creating ArticleDto
public class ArticleDto : Entity
{
/// <summary>
/// 类别Id
/// </summary>
public int? ClassifyId { get; set; }
/// <summary>
/// 类别名称
/// </summary>
public string ClassifyName { get; set; }
public string Content { get; set; }
public string Title { get; set; }
}
3, with IMapper, converted to ArticleDto
List<ArticleDto> articles3 = _articleRepository
.Select
.ToList(r=>new
{
r.Classify,
Article=r
}).Select(r=>
{
ArticleDto articleDto=_mapper.Map<ArticleDto>(r.Article);
articleDto.ClassifyName = r.Classify.ClassifyName;
return articleDto;
}).ToList();
- Also use IMapper conversion, but here Include go, use a little bit different.
Document describes Include "greedy loaded navigation properties, if the query has been used a.Parent.Parent similar expressions, you may not need to do this ..
Here that the query uses a.Parent.Parent, refer to the above 3, ToList in
.ToList(r=>new
{
r.Classify,
Article=r
})
r.Classify, generates Join function. If you do not want to select data ToList desired, may be used as the data need to be correlated Include removed. Then use the Select Linq to convert data in the back, the back should be noted r.Classify may be null, need? Take. Because ClassifyId optional items.
List<ArticleDto> articles4 = _articleRepository
.Select
.Include(r => r.Classify)
.ToList().Select(r =>
{
ArticleDto articleDto = _mapper.Map<ArticleDto>(r);
articleDto.ClassifyName = r.Classify?.ClassifyName;
return articleDto;
}).ToList();
Direct Join
- Not use association property acquisition Columns, Classify attribute class Article in the table List and Classify this time
You can delete,
List<ArticleDto> articleDtos = _articleRepository
.Select
.From<Classify>((a, b) =>a.LeftJoin(r => r.ClassifyId == b.Id)
).ToList((s, a) => new
{
Article = s,
a.ClassifyName
})
.Select(r =>
{
ArticleDto articleDto = _mapper.Map<ArticleDto>(r.Article);
articleDto.ClassifyName = r.ClassifyName;
return articleDto;
}).ToList();
Use SQL direct access to the name of the article and its classification
6.SQL is_deleted need to increase their own judgment.
List<ArticleDto> t9 = _freeSql.Ado.Query<ArticleDto>($@"
SELECT a.*,b.item_name as classifyName
FROM blog_article a
LEFT JOIN base_item b
on a.classify_id=b.id where a.is_deleted=0"
);
to sum up
The number of rows of data taken over the same.
Many.
- Write SQL, very simple.
- Join ORM to use, and then with Mapper becomes complicated.
- Use the navigation property, take the associated data, a InClude to solve the problem
- Use the navigation property, take the associated data, and then with Mapper, basically depends on your Linq, AutoMapper the level. Haha.
Above a certain value, such as the class Article Classify the converted ArticleDto taken in ClassifyName