【问题标题】:CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<CS0029:无法隐式转换类型“System.Collections.Generic.List<
【发布时间】:2017-10-06 18:52:58
【问题描述】:

我正在使用此代码,但不起作用

List<UrunListesi> DegiskenListesi = new List<UrunListesi>();
DegiskenUrunListesi = (from UL in DB.UrunListesi
                       where IDListesi.Contains(UL.KategoriID.ToString()) && UL.Durum == true
                       orderby UL.id descending
                       select UL).ToList();



浏览器错误

"CS0029: 无法隐式转换类型 System.Collections.Generic.List&lt;UrunListesi&gt; [c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll]' 到 System.Collections.Generic.List&lt;UrunListesi&gt; [c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll]'"



我试过这条线,但这也没有用

IEnumerable<UrunListesi> DegiskenUrunListesi = null;
DegiskenUrunListesi = (from UL in DB.UrunListesi
                       where IDListesi.Contains(UL.KategoriID.ToString()) && UL.Durum == true
                       orderby UL.id descending
                       select UL).ToList();



数据库上下文

public partial class MySiteDBEntities : DbContext
    {
        public MySiteDBEntities()
            : base("name=MySiteDBEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<AnaBanner> AnaBanner { get; set; }
        public virtual DbSet<BankaListesi> BankaListesi { get; set; }
        public virtual DbSet<KategoriListesi> KategoriListesi { get; set; }
        public virtual DbSet<KrediKartiOdemeleri> KrediKartiOdemeleri { get; set; }
        public virtual DbSet<TaksitOranTablosu> TaksitOranTablosu { get; set; }
        public virtual DbSet<UrunResimListesi> UrunResimListesi { get; set; }
        public virtual DbSet<UrunYorumlari> UrunYorumlari { get; set; }
        public virtual DbSet<MarkaListesi> MarkaListesi { get; set; }
        public virtual DbSet<IlceListesi> IlceListesi { get; set; }
        public virtual DbSet<SehirListesi> SehirListesi { get; set; }
        public virtual DbSet<UlkeListesi> UlkeListesi { get; set; }
        public virtual DbSet<UyeBilgileri> UyeBilgileri { get; set; }
        public virtual DbSet<KullaniciListesi> KullaniciListesi { get; set; }
        public virtual DbSet<SepetListesi> SepetListesi { get; set; }
        public virtual DbSet<BinListesi> BinListesi { get; set; }
        public virtual DbSet<RenkListesi> RenkListesi { get; set; }
        public virtual DbSet<UrunListesi> UrunListesi { get; set; }
    }



UrunListesi 类

public partial class UrunListesi
{
    public int id { get; set; }
    public Nullable<int> MarkaID { get; set; }
    public Nullable<int> KategoriID { get; set; }
    public string UrunAdi { get; set; }
    public string UrunKodu { get; set; }
    public string UrunAciklama { get; set; }
    public string AnaResim { get; set; }
    public Nullable<decimal> Fiyat { get; set; }
    public Nullable<bool> Indirimli { get; set; }
    public Nullable<decimal> IndirimliFiyat { get; set; }
    public Nullable<int> Kdv { get; set; }
    public Nullable<bool> Iskontolu { get; set; }
    public Nullable<int> IskontoOrani { get; set; }
    public Nullable<int> StokAdet { get; set; }
    public Nullable<int> RenkID { get; set; }
    public Nullable<bool> EviminoImalati { get; set; }
    public Nullable<bool> KargoBedava { get; set; }
    public Nullable<decimal> KargoFiyati { get; set; }
    public Nullable<int> KargoSuresi { get; set; }
    public Nullable<bool> FirsatUrunu { get; set; }
    public Nullable<System.DateTime> FirsatBaslangicTarihi { get; set; }
    public Nullable<System.DateTime> FirsatBitisTarihi { get; set; }
    public Nullable<bool> KampanyaliUrun { get; set; }
    public Nullable<System.DateTime> KampanyaBitis { get; set; }
    public Nullable<int> UrunHit { get; set; }
    public Nullable<System.DateTime> KayitTarih { get; set; }
    public Nullable<bool> Durum { get; set; }
    public Nullable<System.Guid> UrunKey { get; set; }
}

需要帮助,谢谢。

【问题讨论】:

  • 很奇怪,这对我来说没有意义。如果你只使用var 会发生什么?您确定这是产生错误的行吗?您发布的异常表明两种完全相同的类型。
  • IEnumerable&lt;UrunListesi&gt;DB.UrunListesi 是指同一个类型吗?我对此表示怀疑。
  • 请按显示的错误消息准确发布。首选复制和粘贴。
  • 尝试 ToList() 而不是 ToList();
  • 此代码有时不起作用。当点击 login.aspx 页面时,当错误页面返回时没有问题。在职的。我认为问题不在于这段代码?对不起,英语不好:)

标签: c# asp.net generics ienumerable


【解决方案1】:

我解决了这个问题。

我在 dbcontext 中有 UrunListesi 类。我有同名的 UrunListesi.aspx 文件。这就是整个问题。

感谢所有回答。

【讨论】:

    【解决方案2】:

    尝试:

    var list = (from UL in DB.UrunListesi
                where IDListesi.Contains(UL.KategoriID.ToString()) && UL.Durum == true
                orderby UL.id descending
                select UL).ToList();
    

    【讨论】:

      猜你喜欢
      • 2019-10-03
      • 1970-01-01
      • 2020-10-04
      • 2022-08-23
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多