搜索列表中的特定记录时出错 [英] Error while searching for a specific record in a list

查看:69
本文介绍了搜索列表中的特定记录时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表中搜索特定记录



I am searching for a specific record in the list

List<Customer> IService1.get(string str)
       {
         var result = cutomerList.Find(x => x.CustomerName == str);
         return result;
       }





但是当我返回结果时收到如下错误





but i am getting an error like below when i return the result

Cannot implicitly convert type 'WCFExample.Customer' to 'System.Collections.Generic.List<WCFExample.Customer





这是我的清单





here is my list

public static List<Customer> cutomerList = new List<Customer>()
        {
       new Customer {CustomerID = 1, CustomerName="Sujeet",
       Address="Pune", EmailId="test@yahoo.com" },
       new Customer {CustomerID = 2, CustomerName="Rahul",
       Address="Pune", EmailId="test@yahoo.com" },
       new Customer {CustomerID = 3, CustomerName="Mayur",
       Address="Pune", EmailId="test@yahoo.com"}
        };

推荐答案

List.Find 从中返回单个项目列表,但您的 IService1.get 列表< Customer> ,因为它是返回类型。

将返回类型更改为 string ;



List.Find returns a single item from the list, but your IService1.get has List<Customer> as it's return type.
Change the return type to string;

Customer IService1.get(string str)
{
   var result = cutomerList.Find(x => x.CustomerName == str);
   return result;
}





您正在寻找单个项目,因此返回项目列表的方法毫无意义。



列表< T> .Find 找到一个项目(或没有),所以你的方法需要返回相同类型。



希望这会有所帮助,

Fredrik



You're looking for a single item so it makes no sense for the method to return a list of items.

List<T>.Find finds a single item (or none), so your method needs to return the same type.

Hope this helps,
Fredrik


这是一个简单的如果您只是阅读异常并查看您的代码。



您的属性代码返回一个Customer对象,属性定义说它将返回List< customer> ;。该错误表示您无法将Customer隐式转换为List< customer>。所以你认为应该做些什么来解决这个问题?



如果它应该返回一个客户而不是它们的列表,你认为你应该怎么做?正在改变什么?
This is an easy one if you just read the exception and look at your code.

Your property code is returning a single Customer object and the property definition say it's going to return a List<customer>. The error says that you can't implicitly convert a Customer to a List<customer>. So what do you think you should do to fix that??

If it should be returning a single customer and not a list of them, what do you think you should be changing and to what??


当我在列表中搜索时,我必须得到该特定人的记录。



我很困惑:\
When i search in the list i have to get the record of that specific person.

I am confused :\


这篇关于搜索列表中的特定记录时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆