转换 IEnumerable到 IEnumerable;

发布于 2024-10-12 17:39:33 字数 1269 浏览 2 评论 0原文

我在尝试使用 C# 中的收益返回功能时收到此错误。该错误出现在 Visual Studio 内部的选择上,我不太明白。在我看来,我将字符串转换为 ListItem,然后将批次作为 IEnumerable 返回。我对收益回报和 IEnumerable 的理解可能很不切实际,因此任何帮助将不胜感激。注释的代码是老式的方法,可以正常工作。

无法隐式转换类型 'System.Collections.Generic.IEnumerable' 到 'System.Web.UI.WebControls.ListItem'

public partial class CloseIncident : System.Web.UI.Page
{
    private ClevelandIncidentRepository repo = new ClevelandIncidentRepository();

    protected void Page_Load(object sender, EventArgs e)
    {
        SetDropDown(InitialType, repo.GetMainTypes());
    }

    private void SetDropDown(DropDownList dropDown, IEnumerable<string> items)
    {
        dropDown.Items.Clear();
        dropDown.Text = string.Empty;
        dropDown.Enabled = items.Count() > 0;

        dropDown.Items.AddRange(ToListItem(items).ToArray());
    }

    private IEnumerable<ListItem> ToListItem(IEnumerable<string> results)
    {
        yield return from result in results
                     select new ListItem(result);

        //List<ListItem> items = new List<ListItem>();

        //items.AddRange(from result in results
        //               select new ListItem(result));

        //return items;
    }
}

I'm getting this error which while trying to use the yield return feature in C#. The error appears on the select inside visual studio and I don't really understand it. In my mind I'm converting a string to a ListItem and then returning the lot as an IEnumerable. My understanding of yield return and IEnumerable might very well be off the mark so any help would be appreciated. The commented code is the old school way of doing it which does work properly.

Cannot implicitly convert type
'System.Collections.Generic.IEnumerable'
to
'System.Web.UI.WebControls.ListItem'

public partial class CloseIncident : System.Web.UI.Page
{
    private ClevelandIncidentRepository repo = new ClevelandIncidentRepository();

    protected void Page_Load(object sender, EventArgs e)
    {
        SetDropDown(InitialType, repo.GetMainTypes());
    }

    private void SetDropDown(DropDownList dropDown, IEnumerable<string> items)
    {
        dropDown.Items.Clear();
        dropDown.Text = string.Empty;
        dropDown.Enabled = items.Count() > 0;

        dropDown.Items.AddRange(ToListItem(items).ToArray());
    }

    private IEnumerable<ListItem> ToListItem(IEnumerable<string> results)
    {
        yield return from result in results
                     select new ListItem(result);

        //List<ListItem> items = new List<ListItem>();

        //items.AddRange(from result in results
        //               select new ListItem(result));

        //return items;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

婴鹅 2024-10-19 17:39:33

您不需要yield return from ...而只需return from ...或者您可以:

private IEnumerable<ListItem> ToListItem(IEnumerable<string> results)
{
    return results.Select(s => new ListItem(s));
}

或者甚至摆脱这个函数并且:

dropDown.Items.AddRange(items.Select(s => new ListItem(s)).ToArray());

You don't need to yield return from ... but simply return from ... or you could:

private IEnumerable<ListItem> ToListItem(IEnumerable<string> results)
{
    return results.Select(s => new ListItem(s));
}

or even get rid of this function and:

dropDown.Items.AddRange(items.Select(s => new ListItem(s)).ToArray());
短叹 2024-10-19 17:39:33
private IEnumerable<ListItem> ToListItem(IEnumerable<string> results)
    {
        return results.Select(x => new ListItem(x));
    }
private IEnumerable<ListItem> ToListItem(IEnumerable<string> results)
    {
        return results.Select(x => new ListItem(x));
    }
花伊自在美 2024-10-19 17:39:33

yield return 应该引用单个 ListItem,当迭代 IEnumerable 时,该 ListItem 将以“惰性”方式返回。您的 linq 表达式的类型为 IEnumerable,因此存在不匹配。

yield return should refer to a single ListItem which would be returned in a 'lazy' way when the IEnumerable is iterated. Your linq expression is of type IEnumerable so there is a mismatch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文