SQL 查询中的项目参数到对象
Project parameter from SQL query into object
我有以下使用 Dapper 在 C# 中执行的查询。
public async Task<IEnumerable<UserDetails>> GetUsers(DateTime from, DateTime to)
{
try
{
const string query = @"SELECT UserName, ExecutedFromDate as @fromDate
FROM UserTable
WHERE TransactionDate >= @fromDate
AND TransactionDate <= @toDate";
var result = await Connection.QueryAsync<UserDetails>(query, new { fromDate = from, toDate= to });
}
}
我也有一个 POCO class:
public class UserDetails
{
public string UserName { get; set; }
public DateTime ExecutedFromDate { get; set; }
public DateTime ExecutedToDate { get; set; }
}
由于 UserDetails
中的 UserName
已填充,我希望 ExecutedFromDate
和 ExecutedToDate
保留 to
和 [=18 的值=]分别来自参数。我怎样才能做到这一点?请帮忙。
看起来你的 sql 不正确。
SELECT UserName, ExecutedFromDate as @fromDate
在上面,您使用参数作为别名。如果您想 return 您的输入参数作为查询数据,请执行此操作
SELECT UserName, @fromDate fromDate, @toDate toDate...
只需将您的查询更改为:
const string query = @"SELECT UserName,
@fromDate ExecutedFromDate,
@toDate ExecutedToDate
FROM UserTable
WHERE TransactionDate >= @fromDate
AND TransactionDate <= @toDate";
我有以下使用 Dapper 在 C# 中执行的查询。
public async Task<IEnumerable<UserDetails>> GetUsers(DateTime from, DateTime to)
{
try
{
const string query = @"SELECT UserName, ExecutedFromDate as @fromDate
FROM UserTable
WHERE TransactionDate >= @fromDate
AND TransactionDate <= @toDate";
var result = await Connection.QueryAsync<UserDetails>(query, new { fromDate = from, toDate= to });
}
}
我也有一个 POCO class:
public class UserDetails
{
public string UserName { get; set; }
public DateTime ExecutedFromDate { get; set; }
public DateTime ExecutedToDate { get; set; }
}
由于 UserDetails
中的 UserName
已填充,我希望 ExecutedFromDate
和 ExecutedToDate
保留 to
和 [=18 的值=]分别来自参数。我怎样才能做到这一点?请帮忙。
看起来你的 sql 不正确。
SELECT UserName, ExecutedFromDate as @fromDate
在上面,您使用参数作为别名。如果您想 return 您的输入参数作为查询数据,请执行此操作
SELECT UserName, @fromDate fromDate, @toDate toDate...
只需将您的查询更改为:
const string query = @"SELECT UserName,
@fromDate ExecutedFromDate,
@toDate ExecutedToDate
FROM UserTable
WHERE TransactionDate >= @fromDate
AND TransactionDate <= @toDate";