【问题标题】:how to convert list object to list string( getting error Unable to cast object of type)如何将列表对象转换为列表字符串(出现错误无法转换类型的对象)
【发布时间】:2014-11-02 14:46:34
【问题描述】:

嗨,朋友们,我有一个对象列表private static List<Transaction> transactions;

我正在通过列表查询以使用某些条件过滤数据。但我无法返回列表字符串。我收到了错误

无法转换类型的对象 f__AnonymousType1`6[System.Int32,System.String,System.String,System.String,System.String,System.String] 输入“System.String”。

我的计划是让 datagridview 源这个列表像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;
       }

我的类文件是

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; }        
}

请给我一个想法来得到结果

【问题讨论】:

  • 您正在创建一个匿名对象。为什么您希望它可以转换为string?我认为您可能真的想返回List&lt;Transaction&gt;
  • 当我这样做时,我得到错误无法将类型'System.Collections.Generic.List'隐式转换为'System.Collections.Generic.List'
  • @JineshSam 在返回的这些字符串中你需要什么?
  • 为什么不只是return transactions.Select(t =&gt; t.BankName == bankname).ToList();
  • @takemyoxygen 我收到此错误错误 1 ​​无法将类型 'System.Collections.Generic.List' 隐式转换为 'System.Collections.Generic.List'跨度>

标签: c# .net linq


【解决方案1】:

无需将整个集合投影到Anonymous Object。 您实际上所做的只是通过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;
}

【讨论】:

  • 当您的查询调用 ToList() 时,您不需要创建新的 List()。对于进入 if 块的调用者,您将创建一个列表两次。
  • Enumerable.Where 返回 IEnumerable&lt;Transaction&gt;,而不是 List&lt;Transaction&gt;。这就是我打电话给ToList的原因。
  • @Yuval Itzchakov 现在没有错误,但数据网格视图中没有要显示的项目
  • @JineshSam 这可能是因为您的DataGridView 不知道如何表示自定义对象。
  • @StephenKennedy 我明白,但我认为这不是问题的重点。
【解决方案2】:

您无需转换为字符串即可将此结果用作数据源(尽管如果您确实需要字符串,我可以向您展示如何创建格式化字符串而不是匿名类对象)。你可能需要这样的东西:

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>();
       }
  }

【讨论】:

  • 无需为每个对象投影一个新的Transactiontranscations 已经是 List&lt;Transaction&gt;
  • 我不是。这就是有效的 else 块。
  • 对不起,我不明白你的评论。什么else 块?
  • 在 if 中,我返回 query.ToList()。方法执行在该点结束。未命中 if 的调用者将返回 new List();。我现在通过添加一个 else 块来明确这一点。
  • 我不是在说这个。您的 LINQ 查询为每个事务对象生成了一个 new Transcation。这是不必要的,因为源集合已经是 List&lt;Transaction&gt;。看我的回答。
猜你喜欢
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 2015-02-25
  • 2018-05-01
  • 1970-01-01
相关资源
最近更新 更多