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... :/
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:
Sorry for being late to the party, but I after finding this, I thought this should be shared as it's the cleanest, fastest and also memory-saving implementation I could find.
Adapted to your example, you'd write:
The big advantages here (as Damien Guard pointed out in the comments at the link) are:
var foo = createPayments(bar);
as well as usage via myIQueryable.ToPayments() possible.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:
You can try to do the same, but using the methods of extension. What is the provider of the database use?
In addition to the aforementioned methods, you can also parse it as an Enumerable collection, like so:
This also has the added benefit of making life easier when building an anonymous object, like this:
Remembering, however, that parsing a collection as Enumerable pulls it into memory, so it can be resource intensive! Caution should be used here.
I had the same problem today and my solution was similar to what Yoda listed, however it only works with fluent syntax.
Adapting my solution to your code: I added the following static method to the object class
and then updated the base query to the following:
This is logically equivalent to James Manning's solution with the advantage of pushing the bloat of member initialization to the Class / Data Transfer Object
Note: Originally I was using more descriptive names that "Initializer" but after reviewing how I was using it, I found that "Initilizer" was sufficient (at least for my purposes).
Final Note:
After coming up with this solution I was originally thinking it would be simple to share the same code and adapt this to work for Query syntax as well. I am no longer believe that to be the case. I think that if you want to be able to use this type of shorthand construction you would need a method for each (query,fluent) fluent as described above which can exist in the object class itself.
For query syntax an extension method (or some method outside of the base class being used) would be required. (since query syntax wants to operate an IQueryable rather than T)
Here is a sample of what I used to finally get this to work for query syntax. (Yoda already nailed this but I think the usage could be clearer because I didn't get it at first)
and the usage