0

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

无法将类型 '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

        }
    }
4

5 回答 5

1

您正试图List<SearchResults>在 web 方法期望时返回List<string>,您需要确保返回,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());
}
于 2013-07-10T08:51:59.047 回答
0

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

如果您想返回名称列表,请执行以下操作:

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

姓氏列表将是:

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

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

于 2013-07-10T08:52:02.380 回答
0

您返回lsSearchResults的是 typeSearchResults但该方法期望List<string>返回 a 。

您可以将返回类型更改为SearchResults

public static List<SearchResults> QuerySearch() {

}

或者返回一个List<string>

return lsSearchResults.Select(n => n.Name).ToList();
于 2013-07-10T08:52:06.543 回答
0

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

改变它从

public static List<string> QuerySearch()

public static List<SearchResults> QuerySearch()
于 2013-07-10T08:54:19.030 回答
0

方法

public static List<string> QuerySearch()
{
}

需要返回 aList<string>而你正在返回 a List<SearchResults>

更改返回类型以匹配您返回的内容,反之亦然。

所以

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

或者

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

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

于 2013-07-10T08:54:59.970 回答