如何 map/join 两个表并使用 dapper 按参数过滤
How to map/join two tables and filter by parameters using dapper
我想加入两个相互有关系的table,
在客户table中,有两列用于删除和批准。获取已批准但未删除的客户,然后使用 CustId (两个表都有 CustId) 并按提供的参数过滤
将客户与贷款 table 匹配
- 已删除=0
- 批准=1
传递的参数是(并且可以是可选的)用户只能传递一个或两个或两个以上
利率
贷款信托日期
最后付款日期
public async Task<IEnumerable<Loan>> ManageGettingAll(string InterestRate, string LoanTrfDate, string LastPaymentDate)
{
IEnumerable<Loan> loans = null;
//var sql = "Select * from MonthlyInterest where LoanAccountNo=@Id AND Deleted=@Deleted AND CAST(TranDate AS DATE) BETWEEN @StartDate AND @EndDate";
try{
using (var conn = new SqlConnection(connectionstring))
{
await conn.OpenAsync();
var parameters = new
{
InterestRate = InterestRate,
LoanTrfDate = LoanTrfDate,
LastPaymentDate = LastPaymentDate
};
string sql = "Select l.*, c.FirstName_CompanyName from dbo.Loan l left join Customer where and l.Deleted = 0 and and l.Approved = 1 c on l.CustId=c.CustId Where InterestRate=@InterestRate AND CAST(LoanTrfDate AS DATE)=@LoanTrfDate AND CAST(LastPaymentDate AS DATE)=@LastPaymentDate";
loans = await conn.QueryAsync<Loan>(sql, parameters);
}
}
catch (Exception ex)
{
throw;
}
return loans;
}
仅当参数不为空时才按参数过滤,您可以使用下面的SQL。
Select l.*, c.FirstName_CompanyName
from dbo.Loan l
left join Customer c on l.CustId=c.CustId
where l.Deleted = 0 and l.Approved = 1
and (@InterestRate is null or InterestRate=@InterestRate)
AND (@LastPaymentDate is null or (CAST(LoanTrfDate AS DATE)=@LoanTrfDate AND CAST(LastPaymentDate AS DATE)=@LastPaymentDate))
要检查是否至少设置了一个参数,您可以添加此 if 语句。
if(string.IsNullOrWhiteSpace(LoanTrfDate) && string.IsNullOrWhiteSpace(LastPaymentDate))
{
throw new Exception("Some clear message here.");
}
我想加入两个相互有关系的table, 在客户table中,有两列用于删除和批准。获取已批准但未删除的客户,然后使用 CustId (两个表都有 CustId) 并按提供的参数过滤
将客户与贷款 table 匹配- 已删除=0
- 批准=1
传递的参数是(并且可以是可选的)用户只能传递一个或两个或两个以上 利率 贷款信托日期 最后付款日期
public async Task<IEnumerable<Loan>> ManageGettingAll(string InterestRate, string LoanTrfDate, string LastPaymentDate)
{
IEnumerable<Loan> loans = null;
//var sql = "Select * from MonthlyInterest where LoanAccountNo=@Id AND Deleted=@Deleted AND CAST(TranDate AS DATE) BETWEEN @StartDate AND @EndDate";
try{
using (var conn = new SqlConnection(connectionstring))
{
await conn.OpenAsync();
var parameters = new
{
InterestRate = InterestRate,
LoanTrfDate = LoanTrfDate,
LastPaymentDate = LastPaymentDate
};
string sql = "Select l.*, c.FirstName_CompanyName from dbo.Loan l left join Customer where and l.Deleted = 0 and and l.Approved = 1 c on l.CustId=c.CustId Where InterestRate=@InterestRate AND CAST(LoanTrfDate AS DATE)=@LoanTrfDate AND CAST(LastPaymentDate AS DATE)=@LastPaymentDate";
loans = await conn.QueryAsync<Loan>(sql, parameters);
}
}
catch (Exception ex)
{
throw;
}
return loans;
}
仅当参数不为空时才按参数过滤,您可以使用下面的SQL。
Select l.*, c.FirstName_CompanyName
from dbo.Loan l
left join Customer c on l.CustId=c.CustId
where l.Deleted = 0 and l.Approved = 1
and (@InterestRate is null or InterestRate=@InterestRate)
AND (@LastPaymentDate is null or (CAST(LoanTrfDate AS DATE)=@LoanTrfDate AND CAST(LastPaymentDate AS DATE)=@LastPaymentDate))
要检查是否至少设置了一个参数,您可以添加此 if 语句。
if(string.IsNullOrWhiteSpace(LoanTrfDate) && string.IsNullOrWhiteSpace(LastPaymentDate))
{
throw new Exception("Some clear message here.");
}