Search code examples
c#.netautomapperlinq2db

linq2db select and automapper


I have code like this

            return dbConnection.SomeLogTable
                .OrderByDescending(logRecord=> logRecord.Timestamp)
                .Select(dto => _mapper.Map<JournalRecord>(dto))
                .Skip(offset)
                .Take(pageSize)
                .ToList();

I wonder how this code manages to produce correct SQL query without full table fetch. It obviously can't translate select into SQL as it uses AutoMapper method, so how it works?

UPD. Query 10 items that linq2db logs show:

DECLARE @take Integer -- Int32
SET     @take = 10
DECLARE @skip Integer -- Int32
SET     @skip = 0

SELECT
        *all fields listed
FROM
        someLogTable dto
ORDER BY
        dto."timestamp" DESC
LIMIT :take OFFSET :skip

Solution

  • You have to be carefull in that case, some c# function are mapped as sql convertible and some are not, and that may have huge impact on performance when you use it in wrong order.

    For instance: If an used function is not covertible and you use it earlier than other convertible function, it will take more rows than u expect.

    According to your question: how to undestand what is convertable and what's not?

    It has to implement `IQueryable' interface.

    If you are not sure, you can always right click it in Visual Studio -> Go To Definition, and you will notice that for example: Where and Union are sql convertible functions, because they implement `IQueryable' interface.

    IQueryable extensions