如何编写复杂的SQL但使用谓词(通过Dapper)

时间:2016-06-15 15:24:19

标签: c# sql dapper predicate dapper-extensions

Dapper库与Sql Server一起使用。是否可以使用PredicatesComplex Sql Queries传递必需的参数,以便将Sql Resultset过滤为Single Entity Class

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

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

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吗?

  

注意:上面的Sql示例只是一个简单的查询,我有复杂的   使用>查询10个表连接。

1 个答案:

答案 0 :(得分:1)

我建议创建一个视图

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

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