Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, March 17, 2016

Only parameterless constructors and initializers are supported in LINQ to Entities

Only parameterless constructors and initializers are supported in LINQ to Entities


I have this error in this linq expression :

var naleznosci = (from nalTmp in db.Naleznosci                                where nalTmp.idDziecko == idDziec                                select new Payments                                (                                    nalTmp.Dziecko.Imie,                                    nalTmp.Dziecko.Nazwisko,                                    nalTmp.Miesiace.Nazwa,                                    nalTmp.Kwota,                                    nalTmp.RodzajeOplat.NazwaRodzajuOplaty,                                    nalTmp.RodzajeOplat.TypyOplat.NazwaTypuOplaty,                                    nalTmp.DataRozliczenia,                                    nalTmp.TerminPlatnosci                                )).ToList();  

Any idea how solve this problem? I try with any combination of expression... :/

Answer by Muad'Dib for Only parameterless constructors and initializers are supported in LINQ to Entities


yeh, try it like this....

var naleznosci = (from nalTmp in db.Naleznosci                                where nalTmp.idDziecko == idDziec                                select new Payments()                                {                                    Dziecko.Imie,                                    Dziecko.Nazwisko,                                    Miesiace.Nazwa,                                    Kwota,                                    RodzajeOplat.NazwaRodzajuOplaty,                                    RodzajeOplat.TypyOplat.NazwaTypuOplaty,                                    DataRozliczenia,                                    TerminPlatnosci                                }).ToList();  

this will new up your Payment object using a parameterless constructor, and then initialize the properties that are listed inside the curly braces { }

Answer by James Manning for Only parameterless constructors and initializers are supported in LINQ to Entities


without more info on 'Payments' this doesn't help much, but assuming you want to create a Payments object and set some of its properties based on column values:

var naleznosci = (from nalTmp in db.Naleznosci                                where nalTmp.idDziecko == idDziec                                select new Payments                                {                                    Imie = nalTmp.Dziecko.Imie,                                    Nazwisko = nalTmp.Dziecko.Nazwisko,                                    Nazwa= nalTmp.Miesiace.Nazwa,                                    Kwota = nalTmp.Kwota,                                    NazwaRodzajuOplaty = nalTmp.RodzajeOplat.NazwaRodzajuOplaty,                                    NazwaTypuOplaty = nalTmp.RodzajeOplat.TypyOplat.NazwaTypuOplaty,                                    DataRozliczenia = nalTmp.DataRozliczenia,                                    TerminPlatnosci = nalTmp.TerminPlatnosci,                                }).ToList();  

Answer by m-r Tarakanoff for Only parameterless constructors and initializers are supported in LINQ to Entities


You can try to do the same, but using the methods of extension. What is the provider of the database use?

var naleznosci = db.Naleznosci                            .Where(nalTmp => nalTmp.idDziecko == idDziec)                            .Select(                               delegate(TSource nalTmp) { return new Payments                               (                                   nalTmp.Dziecko.Imie,                                   nalTmp.Dziecko.Nazwisko,                                   nalTmp.Miesiace.Nazwa,                                   nalTmp.Kwota,                                   nalTmp.RodzajeOplat.NazwaRodzajuOplaty,                                   nalTmp.RodzajeOplat.TypyOplat.NazwaTypuOplaty,                                   nalTmp.DataRozliczenia,                                   nalTmp.TerminPlatnosci                               ); })                            .ToList();  

Answer by Gene C for Only parameterless constructors and initializers are supported in LINQ to

Entities

Having just encountered this error myself, I thought I would add that if the Payment type is a struct, you would also encounter the same error because struct types do not support parameterless constructors.

In that event, converting Payment to a class and using the object initializer syntax will resolve the issue.

Answer by Tony for Only parameterless constructors and initializers are supported in LINQ to Entities


If you still want to use your constructor for initialization and not properties (sometimes this behaviour is desired for initialization purposes), enumerate the query by calling ToList() or ToArray(), and then use Select(?). Thus it will use LINQ to Collections and that limitation of not being able to call constructor with parameters in Select(?) will vanish.

So your code should look something like this:

var naleznosci = db.Naleznosci                            .Where(nalTmp => nalTmp.idDziecko == idDziec)                            .ToList() // Here comes transfer to LINQ to Collections.                            .Select(nalImp => new Payments                                (                                    nalTmp.Dziecko.Imie,                                    nalTmp.Dziecko.Nazwisko,                                    nalTmp.Miesiace.Nazwa,                                    nalTmp.Kwota,                                    nalTmp.RodzajeOplat.NazwaRodzajuOplaty,                                    nalTmp.RodzajeOplat.TypyOplat.NazwaTypuOplaty,                                    nalTmp.DataRozliczenia,                                    nalTmp.TerminPlatnosci                                ))                            .ToList();  

Answer by Justin Helgerson for Only parameterless constructors and initializers are supported in LINQ to Entities


If you're like me and don't want to have to populate your properties for each query you're building, there is another way to solve this issue.

var query = from orderDetail in context.OrderDetails              join order in context.Orders on order.OrderId equals orderDetail.orderId              select new { order, orderDetail };  

At this point you have an IQueryable containing an anonymous object. If you want to populate your custom object with a constructor you can simply do something like this:

return query.ToList().Select(r => new OrderDetails(r.order, r.orderDetail));  

Now your custom object (which takes two objects as a parameter) can populate your properties as needed.

Answer by XtraSimplicity for Only parameterless constructors and initializers are supported in LINQ to Entities


In addition to the aforementioned methods, you can also parse it as an Enumerable collection, like so:

(from x in table  ....  ).AsEnumerable()  .Select(x => ...)  

This also has the added benefit of making life easier when building an anonymous object, like this:

 (from x in tableName  select x.obj)  .Where(x => x.id != null)  .AsEnumerable()  .Select(x => new {     objectOne = new ObjectName(x.property1, x.property2),     parentObj = x  })  .ToList();  

Remembering, however, that parsing a collection as Enumerable pulls it into memory, so it can be resource intensive! Caution should be used here.

Answer by Mahesh for Only parameterless constructors and initializers are supported in LINQ to Entities


Also, if you want to use a constructor with multiple objects to initialize, you might get error if no values are returned by Linq.

So you might want to do something like this:

(from x in table_1     join y in table_2     on x.id equals y.id     select new {     val1 = x,     val2 = y  })  .DefaultIfEmpty()  .ToList()  .Select(a => new Val_Constructor(a.val1 != null ? a.val1 : new Val_1_Constructor(),                              a.val2 != null ? a.val2 : new Val_2_Constructor()))  .ToList();  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.