简体   繁体   English

C# 无法将类型“XXXX.Classes.Idophalen”隐式转换为“System.Collections.IEnumerable”

[英]C# Cannot implicitly convert type 'XXXX.Classes.Idophalen' to 'System.Collections.IEnumerable'

I making a project where I need to run an query and get that answer in a list into a Datagrid.我制作了一个项目,我需要在其中运行查询并将该答案从列表中获取到 Datagrid 中。

Here is my code: Main:这是我的代码:主要:

        public UserControlDetails(string Id)
    {
        InitializeComponent();
        var db = new Database();
        var QueriAnswer = db.GetArtikel(Id);
        DGVoedingswaarden.ItemsSource = QueriAnswer;
    }

db.GetArtikel(Id): db.GetArtikel(Id):

        public Idophalen GetArtikel(string id)
    {
        using (var conn = new MySqlConnection(ConnectionString))
        {

                conn.Open();
                var query = "SELECT naam FROM tb_product WHERE Id = '" + id + "'";
                var SelectArtikel = new MySqlCommand(query, conn);
                var reader = SelectArtikel.ExecuteReader();

                while (reader.Read())
                {
                    var Id = reader.GetString(0);
                    var artikel = new Idophalen(Id);
                    return artikel;
                }
        }
        return null;
    }

Idophalen ( In English: GetId ) Idophalen(英文:GetId)

   public  class Idophalen
{
    public string Id { get; set; }

    public Idophalen(string Id)
    {
        this.Id = Id;
    }
    public override string ToString()
    {
        return string.Format("{0}", Id);
    }


}

So the problem is that the code ( DGVoedingswaarden.ItemsSource = QueriAnswer; the error: Cannot implicitly convert type 'XXXX.Classes.Idophalen' to 'System.Collections.IEnumerable'. gives. So I don't know how to fix this or what it means. The other posts on Stackoverflow did not help me so here my case.所以问题是代码 ( DGVoedingswaarden.ItemsSource = QueriAnswer; the error: Cannot implicitly convert type 'XXXX.Classes.Idophalen' to 'System.Collections.IEnumerable'.给出。所以我不知道如何解决这个问题或这是什么意思 Stackoverflow 上的其他帖子对我没有帮助,所以这里是我的案例。

If you need more explanation please leave a comment如果您需要更多解释,请发表评论

Your GetArtikel method returns a single Idophalen .您的GetArtikel方法返回单个Idophalen The ItemsSource property of an ItemsControl can only be set to an IEnumerable such as a List<Idophalen> . ItemsControlItemsSource属性只能设置为IEnumerable例如List<Idophalen> So you may create a list that contains only the single object returned from the database:因此,您可以创建一个仅包含从数据库返回的单个对象的列表:

DGVoedingswaarden.ItemsSource = new List<Idophalen>(1) { QueriAnswer };

The ItemsSource property accepts only an IEnumerable. ItemsSource 属性仅接受 IEnumerable。 GetArtikel() returns an Idophalen which is not an IEnumerable instance. GetArtikel()返回一个Idophalen ,它不是一个 IEnumerable 实例。

you can create a list and add the result to it but it really is not a good solution.您可以创建一个列表并将结果添加到其中,但这确实不是一个好的解决方案。 you should reconsider what you are trying to do is plausible.你应该重新考虑你试图做的事情是否合理。

anyways, this should make it work:无论如何,这应该使它起作用:

var QueriAnswer = db.GetArtikel(Id);
DGVoedingswaarden.ItemsSource = new List<Idophalen>() { QueriAnswer };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法转换为&#39;System.Collections.IEnumerable - cannot convert to 'System.Collections.IEnumerable 无法将类型“project_name.Models.model_name”隐式转换为“System.Collections.IEnumerable”。 存在显式转换 - Cannot implicitly convert type 'project_name.Models.model_name' to 'System.Collections.IEnumerable'. An explicit conversion exists C#:错误-无法将类型&#39;int&#39;隐式转换为&#39;System.Collections.Generic.IEnumerable <double> “ - C#: Error-Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable<double>' C#SQLite通用应用插入错误参数1:无法从“ MemBrainz.MainPage.Mood”转换为“ System.Collections.IEnumerable” - C# SQLite Universal App Insert error Argument 1: cannot convert from 'MemBrainz.MainPage.Mood' to 'System.Collections.IEnumerable' 将类型'System.Dynamic.DynamicObject转换为System.Collections.IEnumerable - Convert type 'System.Dynamic.DynamicObject to System.Collections.IEnumerable 无法初始化类型xy不实现&#39;System.Collections.IEnumerable&#39; - cannot initialise type xy Does not implement 'System.Collections.IEnumerable' 无法将类型System.Collections.Generic.IEnumerable &lt;&gt;隐式转换为bool - Cannot implicitly convert type System.Collections.Generic.IEnumerable<> to bool Linq-无法隐式转换类型&#39;System.Collections.Generic.IEnumerable - Linq - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable “无法隐式转换类型&#39;System.Collections.Generic.IEnumerable <C3S.Models.Permission> &#39;到&#39;布尔&#39;。” - “Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<C3S.Models.Permission>' to 'bool'.” C# CosmosDb 查询错误 CS0266:无法隐式转换类型 'System.Linq.IQueryable <system.collections.generic.list<xxxx> ' 到</system.collections.generic.list<xxxx> - C# CosmosDb Query error CS0266: Cannot implicitly convert type 'System.Linq.IQueryable<System.Collections.Generic.List<xxxx>' to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM