1. Dto mapping query

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>();

Select<Tag>().Limit(10).ToList(a => new TestDto { id = a.Id, name = a.Title });
//相当于先映射 TestDto,再映射 a.Id, a.Title
// Note: prior to v0.11.6, only a.id, a.tate were mapped
// Note: v0.11.20 if new this entity, do not attach all fields

fsql.Select<Song>().ToList(a => new DTO { xxx = a.ext }) 
// Case 1: Append all mappings, then map ext, return List
      

fsql.Select<Song>().ToList(a => new Song { id = a.id }) 
// select * from List
      

fsql.Select<Song>().ToList(a => new { id = a.id }) 
List< anonymous object >

fsql.Select<Song>().ToList(a => new DTO(a.id))
// List
      

fsql.Select<Song>().ToList(a => new DTO(a.id) { xxx = a.ext })
// query id, ext, List
      

fsql.Select<Song>().ToList(a => new Song(a.id))
// select * from List
      

fsql.Select<Song>().ToList(a => new Song(a.id) { xxx = a.ext })
// select * from List
      
Copy the code

This mapping supports single-table/multi-table mapping before querying the data (instead of querying all fields and then memory mapping)

Lookup rules, lookup attribute names, will loop the internal object _tables (grow after join query), the primary table is searched first until the same field is found.

Such as:

A, B, C all have id, Dto {id, a1, a2, b1, b2}, a. id is mapped. You can also specify id = C.ID mapping.

Note: you can map a navigation property directly in the DTO

Navigation attribute ManyToOne/OneToOne

ManyToOne/OneToOne navigation attributes are loaded via ToList(), which takes one argument: includeNestedMembers.

Parameter Description:

False: return the data of level 2 Join.

True: returns the navigation data of all deep joins at all levels.

If the query already uses an expression like a.parent, operations such as LeftJoin can be eliminated.

Such as:

Select<Tag>().Where(a => a.Parent.Name == "1").ToList();
// The expression is automatically processed as LeftJoin without the need to mark the Join
Copy the code

If the navigation property is not used and you want to load it, use the Include method.

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

3. Navigation properties OneToMany/ManyToMany

IncludeMany greedily loads the navigation properties of the collection in two separate queries, reloading the data after ToList.

Select<Tag>().IncludeMany(a => a.Songs).ToList();
// This is the greedy loading of ManyToMany relations
Copy the code

OneToMany is used in the same way

IncludeMany takes a second argument that can be used to decorate before a second query.

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

And then, actually in then, you can go down Include/IncludeMany. You can go down 100 floors if you like.

4, mutation

Variable IncludeMany can be greedily loaded even if the selected navigation attribute is not.

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

Support for joint key relationship specification

Query only the first few pieces of data in each subset to avoid IO performance degradation caused by EfCore loading all data (for example, 2000 comments on an item).

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

Set the subset to return partial fields to avoid the problem of too many subset fields.

Select<Tag>().IncludeMany(a => a.TestManys.Select(b => new TestMany { Title = b.Title ... }));
Copy the code

5, IncludeMany extension method

In ISelect, master and subdata queries must be performed within a single code logic.

How to load child data when primary data already exists in memory? So we added the List<T> extension method as shown in the following example:

new List<Song>(new[] { song1, song2, song3 }).IncludeMany(fsql, a => a.Tags);
Copy the code

This is an extended method (IncludeMany) with the same name as iselec.includemany. The parameters are basically the same (except for the extra IFreeSql object parameters) and the functionality is the same (including the previously mentioned variations).

Series article navigation

  • (1) Introduction

  • (2) Automatic migration of entities

  • (3) Entity characteristics

  • (4) Solid features of Fluent Api

  • (5) Insert data

  • (6) Batch insert data

  • (7) Ignore columns when inserting data

  • (8) Specify columns when inserting data

  • (9) Delete data

  • (x) Update data

  • (11) Update data Where

  • (12) Specify columns when updating data

  • (13) Ignore columns when updating data

  • (14) Batch update data

  • (15) Query data

  • (16) paging query

  • (17) joint table query

  • (18) Navigation attributes

  • (19) multi-table query

  • (20) query where ecascade

  • (21) Query returned data

  • (22) Dto mapping query

  • (23) Grouping and aggregation

  • (24) Introduction To Linq To Sql syntax

  • (25) delayed loading

  • Include, IncludeMany, Dto, ToList

  • (27) the SQL statement has been written, and entity class mapping for the second query

  • (28) Business

  • Lambda expression

  • (30) Reading and writing separation

  • (31) Zoning table

  • (32) Aop

  • CodeFirst type mapping

  • (34) CodeFirst migration instructions

  • CodeFirst custom features

The resources

Beginner’s Guide | “Select” | “Update” | “Insert” | “Delete”
Expression function | “CodeFirst” | “DbFirst” | “The BaseEntity”
“Repository” | “The UnitOfWork” | The Filter | Optimism Lock | “The DbContext”
Unread | Partition table | “The tenants” | The AOP | Black Tech | Update log