Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting only a few columns from a table

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();
        }
like image 691
Beetlejuice Avatar asked Dec 17 '22 11:12

Beetlejuice


1 Answers

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};
like image 80
Vasea Avatar answered Dec 19 '22 01:12

Vasea