Linq方法错误

本文关键字:错误 方法 Linq | 更新日期: 2023-09-27 18:21:51

我正在尝试编写一个Linq方法,我将使用ajax调用该方法。我有以下错误,我不知道如何解决它?

无法隐式转换类型'System.Collections.Generic.List<ChillZARdev.Contents.DesignProfile.SearchResults>'到'System.Collections.Generic.List<string>'D:''LetsWork0''ChillZARdev''ChillZARdev''Contents''Home.aspx。cs

错误在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
        }
    }

Linq方法错误

当web方法需要List<string>时,您正试图返回List<SearchResults>,您需要确保返回List<string>,例如

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());
}

您告诉编译器您将返回一个字符串列表,但实际上返回了一个SearchResults列表。编译器不知道如何将SearchResults列表转换为"字符串"列表。

从更改

public static List<string> QuerySearch()

public static List<SearchResults> QuerySearch()

方法返回类型为List<string>,但返回List<SearchResults>,这在C#中是不允许的

如果你想返回一个名字列表,只需执行:

return lsSearchResults.Select(l => l.Name).ToList();

姓氏列表为:

return lsSearchResults.Select(l => l.SurName).ToList();

或者将方法返回类型更改为List<SearchResults>

您返回的是类型为SearchResultslsSearchResults,但该方法期望返回List<string>

您可以将返回类型更改为SearchResults,如下所示:

public static List<SearchResults> QuerySearch() {
}

或返回List<string>

return lsSearchResults.Select(n => n.Name).ToList();

方法

public static List<string> QuerySearch()
{
}

需要返回List<string>,而您正在返回List<SearchResults>

更改退货类型以匹配您要退货的内容,或者反之亦然。

所以

public static List<string> QuerySearch()
{
    return new List<string>();
}

public static List<SearchResults> QuerySearch()
{
    return new List<SearchResults>();
}

请注意,方法返回类型和您在方法体中返回的类型必须匹配。