Reputation: 721
I'm trying to write a Linq method which i will call using ajax. I have the following error i dont know how to solve it?
Cannot implicitly convert type 'System.Collections.Generic.List<ChillZARdev.Contents.DesignProfile.SearchResults>' to 'System.Collections.Generic.List<string>' D:\LetsWork0\ChillZARdev\ChillZARdev\Contents\Home.aspx.cs
The error is highlighted on the return lsSearchResults;
public static List<SearchResults> lsSearchResults = new List<SearchResults>();
public static DBRelationalDataContext myDB = new DBRelationalDataContext(); public class SearchResults
{
public string Name { get; set; }
public string Surname { get; set; }
public SearchResults(string name, string surname)
{
Name = name;
Surname = surname;
}
}
[WebMethod]
Public static List<string> QuerySearch()
{
var found = (from User in myDB.Memberships
where User.Name.ToLower().Contains(RecordSearch) ||
User.Surname.ToLower().Contains(RecordSearch) ||
(User.Name + " " + User.Surname).ToLower().Contains(RecordSearch) ||
(User.Surname + " " + User.Surname).ToLower().Contains(RecordSearch)
select new SearchResults(User.Name, User.Surname)).ToList();
//// validates items in search query if Exist
if (!ChillZARdev.App_Code.Utilities.IsEmpty(found))
{
// List<string> userRecord = new List<string>();
List<SearchResults> lsSearchResults = new List<SearchResults>();
foreach (var user in found)
{
// userRecord.Add(user.Name );
lsSearchResults.Add(new SearchResults(user.Name, user.Surname));
}
//Repeater1.EnableViewState = true;
return lsSearchResults; // This is where the errror is highlighted
}
}
Upvotes: 0
Views: 93
Reputation: 12966
The method
public static List<string> QuerySearch()
{
}
needs to return a List<string>
and you are returning a List<SearchResults>
.
Either change the return type to match what you are returning or visa versa.
So
public static List<string> QuerySearch()
{
return new List<string>();
}
or
public static List<SearchResults> QuerySearch()
{
return new List<SearchResults>();
}
Note, the method return type and the type you return in the method body must match.
Upvotes: 0
Reputation: 10055
You are telling the compiler you will be returning a List of Strings, but are infact returning a List of SearchResults
. The compiler doesn't know how to convert the List of SearchResults
to a list of 'Strings'.
Change it from
public static List<string> QuerySearch()
To
public static List<SearchResults> QuerySearch()
Upvotes: 0
Reputation: 70786
You're returning lsSearchResults
which is of type SearchResults
but the method is expecting a List<string>
to be returned.
You can change the return type to be SearchResults
like this:
public static List<SearchResults> QuerySearch() {
}
Or return a List<string>
return lsSearchResults.Select(n => n.Name).ToList();
Upvotes: 0
Reputation: 62544
Method return type is List<string>
but you return List<SearchResults>
, this is not allowed in C#
if you wanted return a list of names just do:
return lsSearchResults.Select(l => l.Name).ToList();
List of surnames will be:
return lsSearchResults.Select(l => l.SurName).ToList();
Or change method return type to List<SearchResults>
Upvotes: 0
Reputation: 82136
You are trying to return List<SearchResults>
when the web method expects List<string>
, you need to make sure you return List<string>
e.g.
public class SearchResults
{
public string Name { get; set; }
public string Surname { get; set; }
public SearchResults(string name, string surname)
{
Name = name;
Surname = surname;
}
public override string ToString()
{
return String.Format("{0} {1}", Name, Surname);
}
}
...
[WebMethod]
Public static List<string> QuerySearch()
{
...
return lsSearchResults.Select(i => i.ToString());
}
Upvotes: 1