I had problems using c#, nhibernate and link. In the example bellow, I do a SELECT in the BrandTable, but I need only the "Name" and "Id" columns. But it always do a select with all columns of the table. Using EntityFramework, the same code bellow generates a select with only this two columns.
How to do this in nhibernate?
using (ISession session = MyConnection.GetCurrentSession())
{
var brands = from b in session.QueryOver<BrandTable>().List()
orderby b.Name
select new Brand {Id = b.id, Name = b.Name};
return brands.ToList();
}
You can't use query comprehensions with QueryOver, because it is not a Linq provider. In your example you're actually selecting all records, and then using LINQ to Objects. Add the NHibernate.Linq namespace to your file and rewrite the query as
from b in session.Query<BrandTable>()
orderby b.Name
select new Brand {Id = b.id, Name = b.Name};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With