FreeSql (26) Eager Load Include, IncludeMany, Dto, ToList

Eager Load Gu name Incredibles is to put all the things you want to load a one-time reading.

To cope with this section [delay] to load the birth, and he greedily loaded with the introduction of this process should be two-pronged development projects, in order to write a high-quality program.

Dto mapping query

Select<Tag>().Limit(10).ToList(a => new TestDto { id = a.Id, name = a.Title });
Select<Tag>().Limit(10).ToList(a => new TestDto());
Select<Tag>().Limit(10).ToList(a => new TestDto { });
Select<Tag>().Limit(10).ToList(a => new TestDto() { });
Select<Tag>().Limit(10).ToList<TestDto>();

This mapping supports single table / multi table.

Find a rule, find the attribute name, will cycle internal object _tables (will increase after the join query) to the main table priority check until found in the same field.

Such as:

A, B, C are id, Dto {id, a1, a2, b1, b2}, A.id mapped. You can also specify id = C.id map.

Friendly reminder: dto be directly mapped a navigation property

Navigation attribute ManyToOne / OneToOne

ManyToOne / OneToOne navigation attributes () by loading ToList, this method has one parameter: includeNestedMembers.

Parameter Description:

false: Returns the class 2 Join data;

true: Returns all levels of depth Join the navigation data;

If the query has been used a.Parent.Parent similar expressions, you may not need to LeftJoin and other operations.

Such as:

Select<Tag>().Where(a => a.Parent.Name == "1").ToList();
//这样写,不需要再标记 Join,解析表达式时自动处理成 LeftJoin

If the navigation attribute is not used, want to load, the method may be used Include.

Select<Tag>().Include(a => a.Parent).ToList();

Navigation attribute OneToMany / ManyToMany

IncludeMany greedy loaded navigation properties collection, in fact, in two inquiries conducted data reloading after ToList.

Select<Tag>().IncludeMany(a => a.Songs).ToList();
//这是 ManyToMany 关系的贪婪加载

Using the same method OneToMany

IncludeMany second parameter modifications can be made to work before the second query.

Select<Tag>().IncludeMany(a => a.Songs, 
    then => then.Where(song => song.User == "admin")).ToList();

Then, in fact, then there is also an ongoing process Include / IncludeMany. As long as you like, down 100 layer no problem.

variation

Variation IncludeMany, even if you choose not navigation attributes can also be greedy loaded.

Select<Tag>().IncludeMany(a => a.TestManys.Where(b => b.TagId == a.Id));

Specifying the key support of the United

For example EFCore include it, how to query only the first few data for each subset, it can only lead to poor load all IO performance (if some subset, there are 100, 200), FreeSql can solve this problem.

Select<Tag>().IncludeMany(a => a.TestManys.Take(10));

Guess you like

Origin www.cnblogs.com/FreeSql/p/11531404.html
dto