Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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

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


here is my list

C#
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"}
        };
Posted
Updated 7-Aug-13 2:56am
v2

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;

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


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
 
Share this answer
 
v2
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??
 
Share this answer
 
When i search in the list i have to get the record of that specific person.

I am confused :\
 
Share this answer
 
Comments
Fredrik Bornander 8-Aug-13 3:27am    
I added a code sample on how to change your get method to my answer. Hope this helps.
The problem is i have customer class like

C#
List<customer> get(string str);</customer>


C#
[DataContract]
   public class Customer
   {
       [DataMember]
       public int CustomerID { get; set; }
       [DataMember]
       public string CustomerName { get; set; }
       [DataMember]
       public string Address { get; set; }
       [DataMember]
       public string EmailId { get; set; }
   }


When i tried as u said i am getting an error like
Cannot implicitly convert type 'WCFExample.Customer' to 'System.Collections.Generic.List<wcfexample.customer>'

any help??
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900