C# 如何将列表对象转换为列表字符串(获取错误无法转换类型的对象)
嗨,朋友们,我有一个对象列表C# 如何将列表对象转换为列表字符串(获取错误无法转换类型的对象),c#,.net,linq,C#,.net,Linq,嗨,朋友们,我有一个对象列表私有静态列表事务 我正在通过列表进行查询,以筛选具有某些条件的数据。但是我无法返回列表字符串。我发现了错误 无法强制转换类型为的对象 f__AnonymousType1`6[System.Int32,System.String,System.String,System.String,System.String,System.String,System.String] 键入“System.String” 我的计划是让datagridview源像dataGridView2
私有静态列表事务代码>
我正在通过列表进行查询,以筛选具有某些条件的数据。但是我无法返回列表字符串。我发现了错误
无法强制转换类型为的对象
f__AnonymousType1`6[System.Int32,System.String,System.String,System.String,System.String,System.String,System.String]
键入“System.String”
我的计划是让datagridview源像dataGridView2.DataSource=BasicClass.banksearch(“ABC”)代码>
请告诉我获得结果的想法无需将整个集合投影到匿名对象上
。
实际上,您所做的只是按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;
}
公共静态列表BankSearch(字符串bankname,字符串sdate=null,字符串edate=null,字符串条件=null)
{
List filteredTransactions=新列表();
if(sdate==null&&edate==null)
{
filteredTransactions=transactions.Where(t=>t.BankName==BankName.ToList();
}
返回过滤传输;
}
无需将整个集合投影到匿名对象上
。
实际上,您所做的只是按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;
}
公共静态列表BankSearch(字符串bankname,字符串sdate=null,字符串edate=null,字符串条件=null)
{
List filteredTransactions=新列表();
if(sdate==null&&edate==null)
{
filteredTransactions=transactions.Where(t=>t.BankName==BankName.ToList();
}
返回过滤传输;
}
将此结果用作数据源不需要转换为字符串(尽管如果您确实需要字符串,我可以向您展示如何创建格式化字符串而不是匿名类对象)。您可能需要以下内容:
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>();
}
}
公共静态列表banksearch(字符串bankname,字符串sdate=null,字符串edate=null,字符串条件=null)
{
if(sdate==null&&edate==null)//仅银行
{
返回事务//类型:列表
.Where(t=>t.BankName==BankName)
.ToList();
}否则{
返回新列表();
}
}
将此结果用作数据源不需要转换为字符串(尽管如果您确实需要字符串,我可以向您展示如何创建格式化字符串而不是匿名类对象)。您可能需要以下内容:
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>();
}
}
公共静态列表banksearch(字符串bankname,字符串sdate=null,字符串edate=null,字符串条件=null)
{
if(sdate==null&&edate==null)//仅银行
{
返回事务//类型:列表
.Where(t=>t.BankName==BankName)
.ToList();
}否则{
返回新列表();
}
}
您正在创建一个匿名对象。为什么您希望它可以转换为字符串
?我想你可能真的想返回一个列表
当我这样做时,我得到一个错误:不能隐式地将类型“System.Collections.Generic.List”转换为“System.Collections.Generic.List”@JineshSam返回的字符串中需要什么?为什么不只返回事务。选择(t=>t.BankName==BankName.ToList()代码>?@takemyoxygen我收到此错误错误1无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”您正在创建匿名对象。为什么您希望它可以转换为字符串
?我想你可能真的想返回一个列表
当我这样做时,我得到一个错误:不能隐式地将类型“System.Collections.Generic.List”转换为“System.Collections.Generic.List”@JineshSam返回的字符串中需要什么?为什么不只返回事务。选择(t=>t.BankName==BankName.ToList()代码>?@takemyoxygen我收到此错误错误1无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”无需为每个对象投影新的事务<代码>转换
已经是一个列表
。我不是。这是else块。对不起,我不理解你的评论。什么else
块?在if中,我返回query.ToList()。方法执行在该点结束。未命中if的呼叫者将命中return new List();。我已经通过添加一个else块明确了这一点。我不是在谈论这个。LINQ查询为每个事务对象生成了一个新事务
。这是不必要的,因为源集合已经是一个列表
。请参阅我的答案。无需为每个对象投影新的事务<代码>转换
已经是一个列表
。我不是。这是else块。对不起,我不理解你的评论。什么else
块?在if中,我返回query.ToList()。方法执行在该点结束。未命中if的呼叫者将命中return new List();。我已经通过添加一个else块明确了这一点。我不是在谈论这个。LINQ查询为每个事务对象生成了一个新事务
。这是不必要的,因为源集合已经是一个列表
。查看我的答案。当查询调用ToList()时,不需要创建新的List()。对于进入if块的呼叫者,您将创建一个列表两次。可枚举。其中
返回一个IEnumerable
,而不是列表
。这就是为什么我现在调用ToList
@Yuval-Itzchakov,没有错误,但是没有要在列表中显示的项目datagridview@JineshSam这可能是因为您的DataGridView
不知道如何表示自定义对象。@StephenKennedy我理解,但我认为这不是问题的关键。您不需要在