Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将列表对象转换为列表字符串(获取错误无法转换类型的对象)_C#_.net_Linq - Fatal编程技术网

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我理解,但我认为这不是问题的关键。您不需要在