【发布时间】: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<Transaction> -
当我这样做时,我得到错误无法将类型'System.Collections.Generic.List
'隐式转换为'System.Collections.Generic.List ' -
@JineshSam 在返回的这些字符串中你需要什么?
-
为什么不只是
return transactions.Select(t => t.BankName == bankname).ToList();? -
@takemyoxygen 我收到此错误错误 1 无法将类型 'System.Collections.Generic.List
' 隐式转换为 'System.Collections.Generic.List '跨度>