0

Hi Friends i have a list of objects private static List<Transaction> transactions;

i am querying through the list to filter the data with some criteria. but i am not able to return the list string. i am getting the error

Unable to cast object of type <>f__AnonymousType1`6[System.Int32,System.String,System.String,System.String,System.String,System.String] to type 'System.String'.

my plan is to make the datagridview source this list like dataGridView2.DataSource = BasicClass.banksearch("ABC");

public static List<string> banksearch(string bankname, string sdate = null, string edate = null, string condition = null)
    {
        List<string> returnstr = new List<string>();
        if (sdate == null && edate == null)//only bank
        {
            returnstr = transactions
                .Where(t => t.BankName == bankname)
                .Select(t => new
                 {
                     TransactionID = t.TransactionID,
                     BankName = t.BankName,
                     TemplateModel = t.TemplateModel,
                     Date = t.Date.ToString(),
                     PayeeName = t.PayeeName,
                     Amount = t.Amount.ToString()
                 }).Cast<String>().ToList();
        }
       return returnstr;
       }

my class file is

class Transaction
{
        public int TransactionID { get; set; }
        public string BankName { get; set; }
        public string TemplateModel { get; set; }
        public DateTime Date { get; set; }
        public string PayeeName { get; set; }
        public decimal Amount { get; set; }        
}

Please give me idea to get the result

7
  • 1
    You're creating an anonymous object. Why do you expect it to be convertible to a string? I think you may really want to return a List<Transaction> Commented Nov 2, 2014 at 14:47
  • when i do like this i get error Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#3>' to 'System.Collections.Generic.List<chequenew.Transaction>' Commented Nov 2, 2014 at 14:59
  • @JineshSam what do you need to be in these strings you return? Commented Nov 2, 2014 at 15:00
  • 1
    Why not just return transactions.Select(t => t.BankName == bankname).ToList(); ? Commented Nov 2, 2014 at 15:02
  • @takemyoxygen i am getting this error Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>' Commented Nov 2, 2014 at 15:09

2 Answers 2

3

There is no need to project the entire collection onto an Anonymous Object. All you're actually doing is filtering by bankname:

public static List<Transaction> BankSearch(string bankname, string sdate = null, string edate = null, string condition = null)
{
    List<Transaction> filteredTransactions = new List<Transaction>();
    if (sdate == null && edate == null)
    {
        filteredTransactions = transactions.Where(t => t.BankName == bankname).ToList();
    }

    return filteredTransactions;
}
Sign up to request clarification or add additional context in comments.

6 Comments

You don't need to create new List<Transaction>() when your query is calling ToList(). For callers which enter the if block, you are creating a list twice.
Enumerable.Where returns an IEnumerable<Transaction>, not a List<Transaction>. That's why i call ToList.
@Yuval Itzchakov Now no error but no items to display in the datagridview
@JineshSam That may be because your DataGridView doesn't know how to represent a custom object.
@StephenKennedy I understand, but i think that isn't the point of the question.
|
1

You don't need to convert to a string in order to use this result as a datasource (although if you actually need a string I can show you how to create a formatted string instead of an anonymous class object). You likely need something like this:

public static List<Transaction> banksearch(string bankname, string sdate = null, string edate = null, string condition = null)
    {
        if (sdate == null && edate == null)//only bank
        {
           return transactions // type: List<Transaction>
                .Where(t => t.BankName == bankname)
                .ToList();
        } else {
           return new List<Transaction>();
       }
  }

9 Comments

There's no need to project a new Transaction for each object. transcations is already a List<Transaction>.
I'm not. That's the else block effectively.
Sorry, i didn't understand your comment. what else block?
Inside the if, I return query.ToList(). Method execution ends at that point. Callers which didn't hit the if will hit return new List<Transaction>();. I've made this explicit now by adding an else block.
Im not talking about that. Your LINQ query generated a new Transcation for each transaction object. It's unnecessary as the source collection is already a List<Transaction>. See my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.