Jeffrey Johnston
Jeffrey Johnston

Reputation: 141

Chain multiple WHERE clauses

Depending on conditions, I'm trying to chain multiple WHERE clauses to the TransactionDataList variable. Please advise what's the right way to do this.

I'm getting this error:

Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

Code:

List<Transaction> QueriedTransactionList;
QueriedTransactionList = db.Transactions.ToList();

List<TransactionViewModel> TransactionDataList = QueriedTransactionList.Select(x => new TransactionViewModel
{
    TTransactionID = x.TTransactionID,
    BatchID = x.BatchID,
    TransactionDateTime = x.TransactionDateTime,
    TransactionStatus = x.TransactionStatus,
    TaxPayerName = x.Card.TaxPayer.TaxPayerName,
    TaxPayerEmail = x.Card.TaxPayer.TaxPayerEmail
}).GroupBy(x => x.BatchID).Select(x => x.LastOrDefault()).OrderByDescending(x => x.TTransactionID).ToList();

if (Request.QueryString["Port"] != "")
{
    int Port = Convert.ToInt32(Request.QueryString["Port"]);
    TransactionDataList = TransactionDataList.Where(x => x.Card.PortID == Port);
}

if (Request.QueryString["Status"] != "")
{
    string Status = Request.QueryString["Status"];
    TransactionDataList = TransactionDataList.Where(x => x.TransactionStatus == Status);
}

if (Request.QueryString["TIN"] != "")
{
    string TIN = Request.QueryString["TIN"];
    TransactionDataList = TransactionDataList.Where(x => x.Card.TaxPayerTIN == TIN);
}

Upvotes: 0

Views: 85

Answers (1)

Gusdor
Gusdor

Reputation: 14334

A List<T> is an IEnumerable<T> but an IEnunumerable<T> is not guarenteed to be a list. The return type from Select and Where is IEnumerable<T> and cannot be assigned to TransactionDataList.

Change

List<TransactionViewModel> TransactionDataList = QueriedTransactionList.Select...

to

IEnumerable<TransactionViewModel> TransactionDataList = QueriedTransactionList.Select...

and the errors will go away. If you want access to methods provided by IList<T>, you will need to convert back to list using the ToList extension method.

Upvotes: 5

Related Questions