Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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# 无法隐式转换类型';System.collections.generic.IEnumerable<;项目Dto.付款Dto>';至';字符串';_C#_Asp.net Mvc - Fatal编程技术网

C# 无法隐式转换类型';System.collections.generic.IEnumerable<;项目Dto.付款Dto>';至';字符串';

C# 无法隐式转换类型';System.collections.generic.IEnumerable<;项目Dto.付款Dto>';至';字符串';,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个类和控制器如下 //class public class Payment { public Guid ID { get; set; } public string InvoiceNumber { get; set; } public string ContactNumber { get; set; } public string ConsumerName { get; set; } } //controller public class Tran

我有一个类和控制器如下

 //class
 public class Payment
{  
    public Guid ID { get; set; }
    public string InvoiceNumber { get; set; }
    public string ContactNumber { get; set; }
    public string ConsumerName { get; set; }
}

//controller
public class TransPaymentController : Controller
{

     public ActionResult Searching(int? page,string searchInvoiceNumber)
    {

        //Page Setting
        int pageSize = 10;
        int pageNumber = (page ?? 1);

        IPagedList<Payment> payments = null;
        payments= paymentRepo.GetList_Payments_InTableManualPayment(pageSize, pageNumber);


        if (!String.IsNullOrEmpty(searchInvoiceNumber))
        {

            string data = payments.Where(s => s.InvoiceNumber.Contains(searchInvoiceNumber)); //the error is here

        }
        return null;
    }
}
//类
公共类支付
{  
公共Guid ID{get;set;}
公共字符串InvoiceNumber{get;set;}
公共字符串ContactNumber{get;set;}
公共字符串ConsumerName{get;set;}
}
//控制器
公共类TransPaymentController:控制器
{
公共操作结果搜索(int?页,字符串searchInvoiceNumber)
{
//页面设置
int pageSize=10;
整数页码=(第1页);
IPagedList付款=空;
paymentRepo.GetList\u payments\u InTableManualPayment(页面大小、页码);
如果(!String.IsNullOrEmpty(searchInvoiceNumber))
{
字符串数据=付款。其中(s=>s.InvoiceNumber.Contains(searchInvoiceNumber));//错误在这里
}
返回null;
}
}
我得到一个错误如下

无法将类型
System.collections.generic.IEnumerable隐式转换为字符串


为什么我会犯这个错误

从外观上看,您的控制器只是试图查找发票号。与将集合
IEnumerable
转换为单个元素
字符串
相关的错误性质

考虑您的查询询问数据库的内容:请为我查找发票编号包含
searchInvoiceNumber
的所有发票。数据库可能返回零个、一个或多个结果。作为一根线,它看起来像什么?那是个毫无意义的问题

请尝试:

var data = payments.Where(s => s.InvoiceNumber.Contains(searchInvoiceNumber)).ToList();
现在数据包含零个、一个或多个
PaymentDto
项目。你自己决定怎么处理


请注意,您的
Payment
类目前尚未使用。如果您试图直接从查询中填充,这可能是一个单独的问题。

您将
数据声明为
字符串
,其中
不返回字符串。你到底想得到什么?这可能是一个。正如@Nkosi所提到的,
Where
在您的情况下将返回一个
IEnumerable
。您可以使用投影(如
Select
Aggregate
)将源数据类型转换/投影为目标数据类型(Aggregate可以将每个项合并为单个字符串,文档中也有这样做的示例)在LINQ中定义的方法。如果将
字符串数据
替换为
var数据
,则错误应消失。但正如前面的评论所述,我们不清楚您在这里试图实现什么。@Nkosi即使这是一个XY问题,在这种情况下,Y对于X的任何预期解决方案都是非常基本的。对C#语言有了基本的了解后,应该很清楚为什么会出现错误。所以我不清楚你在问什么,因为我解释了为什么你的问题可能解决不了。