实体框架错误无法İmplicity将类型“布尔”转换为“字符串”

时间:2019-12-20 12:18:22

标签: c# entity-framework model-view-controller

我想将s.UrunNavigation.Uadi与Adi同步,但出现此错误

  

不能将类型'bool'隐式转换为'string'

     

无法将lambda表达式转换为委托类型   'System.Func'因为一些返回   块中的类型不能隐式转换为委托   返回类型

public ActionResult UrunMiktarIcin(string Adi, string OcakUrun) 
{

    string reply;
    if (OcakUrun == "urun")
    {
         reply = _context.UrunMiktari
            .Include(u => u.HangiOcakNavigation)
            .Include(u => u.UrunNavigation)
            .Where(s => s.UrunNavigation.Uadi = Adi)
            .Select(u => new 
            { 
                UrunAdi = u.UrunNavigation.Uadi, 
                OcakAdi = u.HangiOcakNavigation.NeredekiOcak, 
                UrunSayisi = u.UMiktari
            });


    return Json();
}

我的UrunMiktari模型:

public partial class UrunMiktari
    {
        public int Id { get; set; }
        public int Urun { get; set; }
        public int UMiktari { get; set; }
        public int HangiOcak { get; set; }

        public virtual CayOcaklari HangiOcakNavigation { get; set; }
        public virtual Urunler UrunNavigation { get; set; }
    }

我的Urunler模型:

public partial class Urunler
    {
        public Urunler()
        {
            UrunCikis = new HashSet<UrunCikis>();
            UrunGiris = new HashSet<UrunGiris>();
            UrunMiktari = new HashSet<UrunMiktari>();
        }

        public int Id { get; set; }
        public string Uadi { get; set; }
        public decimal SatisFiyati { get; set; }
        public decimal AlisFiyati { get; set; }
        public bool AktifPasif { get; set; }
        public string Barcodu { get; set; }

        public virtual ICollection<UrunCikis> UrunCikis { get; set; }
        public virtual ICollection<UrunGiris> UrunGiris { get; set; }
        public virtual ICollection<UrunMiktari> UrunMiktari { get; set; }
    }

我尝试“ s => s.UrunNavigation.Uadi == Adi”

reply = _context.UrunMiktari.Include(u => u.HangiOcakNavigation).Include(u => u.UrunNavigation).Where(s => s.UrunNavigation.Uadi == Adi).Select(u => new { UrunAdi = u.UrunNavigation.Uadi ,OcakAdi = u.HangiOcakNavigation.NeredekiOcak, UrunSayisi = u.UMiktari })

出现此错误:

  

无法将类型'system.linq.iqueryable <>隐式转换为'字符串'

1 个答案:

答案 0 :(得分:0)

   _context.UrunMiktari.Include(u => u.HangiOcakNavigation).Include(u => u.UrunNavigation).Where(s => s.UrunNavigation.Uadi == Adi).Select(u => new { UrunAdi = u.UrunNavigation.Uadi ,OcakAdi = u.HangiOcakNavigation.NeredekiOcak, UrunSayisi = u.UMiktari })

返回类型IEnumerable<{ UrunAdi, OcakAdi, UrunSayisi }>,而不返回类型string

您似乎想返回一个json数组?

尝试

public ActionResult UrunMiktarIcin(string Adi, string OcakUrun) 
{

   if (OcakUrun == "urun")
   {
      var reply = _context.UrunMiktari
         .Include(u => u.HangiOcakNavigation) // this is not necessary because manually projecting
         .Include(u => u.UrunNavigation) // this is not necessary because manually projecting
         .Where(s => s.UrunNavigation.Uadi == Adi) // Had a single `=`, need `==`.
         .Select(u => new 
         { 
             UrunAdi = u.UrunNavigation.Uadi,
             OcakAdi = u.HangiOcakNavigation.NeredekiOcak, 
             UrunSayisi = u.UMiktari 
         });
      return Json(reply);
   }

   return Json();
}

您还有一个分配操作(=),需要一个比较操作(==)。