Only parameterless constructors and initializers are supported in LINQ to Entities

后端 未结 14 1363
暗喜
暗喜 2020-11-27 15:27

I have this error in this linq expression :

var naleznosci = (from nalTmp in db.Naleznosci
                              where nalTmp.idDziecko == idDziec
           


        
14条回答
  •  失恋的感觉
    2020-11-27 16:12

    First I would avoid the solution with

    from ....
    select new Payments
    {
      Imie = nalTmp.Dziecko.Imie,
      ....
    }
    

    This requires an empty constructor and ignores encapsulation so you are saying new Payments() is a valid payment without any data, but instead the object must have at least a value and probably other required fields depending on your domain.

    It's better to have a constructor for required fields but only bring needed data:

    from ....
    select new
    {
      Imie = nalTmp.Dziecko.Imie,
      Nazwisko = nalTmp.Dziecko.Nazwisko
      ....
    }
    .ToList() // Here comes transfer to LINQ to Collections.
    .Select(nalImp => new Payments
     (
      nalTmp.Imie,//assume this is a required field
      ...........
      )
      {
         Nazwisko = nalTmp.Nazwisko //optional field
      })
    .ToList();
    

提交回复
热议问题