I’m going crazy with this and would greatly appreciate some help.
Imagine two tables connected with a foreign key:
Fonts
FontColors
I need to get a computer FontColors report with the Fonts table info included as well. Via the entity framework I can obviously access font colors “Fonts” table properties the following way:
string fontName = FontColors.Fonts.Name;
Simple…right?
Now imagine I created another class called ComputerFontColors, for example, to match my report model that I’ll display (Jquery Grid report by the way ) that will include that same Font and FontColors information in addition to some info pulled form my Computer description table.
So to populate that ComputerFontColors class we go with something like this:
var computerFonts = from f in FontColors
select new ComputerFontColors
{
FontColor = f.Color,
FontName = f.Fonts.Name,
ComputerUsedOn = ComputerServices.GetByFontId(f.Fonts.Id)
}
Seems to be as simple as it can get but for whatever reason it just doesn’t work. Nhibernate Linq doesn’t like the “ComputerUsedOn = ComputerServices.GetByFontId(f.Fonts.Id)” part and just keeps coming back with with “Could not instantiate: FontFolors ” error.
The method
ComputerServices.GetByFontId(f.Fonts.Id)
by itself works fine.
The query with a static value thrown in:
var computerFonts = from f in FontColors
select new ComputerFontColors
{
FontColor = f.Color,
FontName = f.Fonts.Name,
ComputerUsedOn = ComputerServices.GetByFontId(6)
}
works fine.
But when I combine them together – code crashes.
Been stuck on this one.
Thanks in advance.
For kicks, try this:
AsEnumerable forces the projection to be done using the standard Linq to Objects methods, i.e. in-memory. Could be that Linq to NH has trouble understanding that your projection can’t be done entirely in the database (given that ComputerServices.GetByFontId is a method that can’t be translated to a query).