I wrote a Web API method to get transaction history from the database using a stored procedure. The input includes a table type parameter which gets input from IEnumerable<string> transactionCategories
.
public IEnumerable<ITransactionItem> Get(long loanId, DateTime? fromDate, DateTime? toDate, IEnumerable<string> transactionCategories)
{
IList<ITransactionItem> items;
try
{
var transCategories = transactionCategories.Select(t => new TransCategoryTableType(t));
using (var connector = GetConnector(ConnectionNames.UDSConnection))
{
items = (IList<ITransactionItem>)connector.StoredProcedure("spname")
.Parameter("@LoanId", loanId)
.Parameter("@FromDate", fromDate)
.Parameter("@ToDate", toDate)
.TableValuedParameter("@TransactionCategories", transCategories)
.As<ILedgerTransactionItem>()
.WithMapper(_mappingStrategy)
.GetRows();
}
}
catch (Exception e)
{
throw;
}
return items;
}
I was data like this ['Tax','Interest'], but the stored procedure doesn't take this input and always returns null. I have defined the TransCategoryTableType
as below
public sealed class TransCategoryTableType
{
public TransCategoryTableType(string description)
{
Description = description;
}
public string Description { get; set; }
}
Need help to figure this out. The stored proc initial part looks like below:
ALTER PROCEDURE [Core].[uspLedgerTransactionHistory]
(
@LoanId BIGINT,
@FromDate DATETIME = NULL,
@ToDate DATETIME = NULL,
@TransactionCategories [Reference].[TransCategories] READONLY
)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
CREATE TABLE #Temp(TransCodeId INT)
INSERT INTO #Temp
Select tc.transactioncodeid FROM Reference.tTransactionCodes tc
JOIN Reference.tTransCodeCategories tcc
ON tc.TransactionCodeCategoryId=tcc.TransCodeCategoryId
WHERE tcc.Description IN (SELECT t.Description FROM @TransactionCategories t)