如何编写 Complex SQL 但使用 Predicate(通过 Dapper)

How to write Complex SQL but use Predicate (through Dapper)

正在将 Dapper 库与 Sql Server 一起使用。是否可以使用 PredicatesComplex Sql Queries 来传递所需的参数,以便将 Sql 结果集过滤成 Single Entity Class

SELECT * FROM Config.Country 
INNER JOIN Config.State 
ON Config.State.CountryId = Config.Country.CountryId

实体 class 可以具有以下结构。

public sealed class CountryStateReadDto
{
    //Country table
    public byte CountryId { get; set; }
    public string CountryName { get; set; }
    public string CountryCode { get; set; }
    public string ISOCode { get; set; }
    public string DailingCode { get; set; }
    public string WebCode { get; set; }
    public double Longitude { get; set; }
    public double Latitude { get; set; }

    //State table
    public short StateId { get; set; }
    public string StateName { get; set; }
}

我知道,有一个漂亮的 Dapper-Extension 项目可以完成这项工作,但仅适用于 Single Table only

我需要写 Complex Sql Queries 最好使用 Predicates。有人可以通过提供一些提示来帮助我,甚至解决方案也会非常有帮助!

如果 Predicate 不是一个理想的解决方案,那么还有什么我可以考虑或比 Predicate 更好的吗?

Note: The above Sql example is just a simple query, I have complex queries with > 10 table joins.

我建议创建一个视图

Create View StateDetails
SELECT * FROM Config.Country 
INNER JOIN Config.State 
ON Config.State.CountryId = Config.Country.CountryId

设置视图后,使用 Dapper 会变得更加容易。