10

I have a one class to one table mapping; unfortunately this table has 110+ columns, and queries take a long time process, especially when most of the time I only want to view <10 columns.

My problem is that the queries are dynamically generated based on what the user wants to look at. I can't really create different mappings with different columns because there would be a very large number of combinations. I'm using the criteria API to generate the queries. Can I also use this to only select the columns the user wants? Or some other method?

Thanks

Harry
  • 3,076
  • 4
  • 28
  • 44
  • 16
    What did you say 110 columns ? back to the drawing board ! – V4Vendetta May 24 '11 at 13:34
  • 2
    @V4Vendetta "What? Redesign my precious database? I spent years on perfecting its structure and no one will tell me what to do with it!!!" (c) Your Client... – Spook Aug 02 '16 at 06:00

3 Answers3

17

Easy to do with LINQ (assuming you're using NHibernate 3.0 or later):

var products = from p in Session.Query<Product>()
               where // ...some query (snip)
               select new
               {
                   Name = p.ProductName,
                   Description = p.ShortDesc,
                   Price = p.Price,
                   Units = p.Quantity
               };

Also, if you're using HQL, you can just select the columns you need similar to using T-SQL, but use a Transformer to get a strongly typed object back:

First create a class with your narrowed down columns:

public class ProductReport
{
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public int Units { get; set; }
}

Then your query:

string hql = "select p.ProductName as Name, p.ShortDesc as Description ...(snip) " +
             "from Product p " +
             "where ...some query (snip)";

IQuery query = Session.CreateQuery(hql)
    .SetResultTransformer(Transformers.AliasToBean<ProductReport>());

IList<ProductReport> products = query.List<ProductReport>();

Just sure make the aliases in your query (as Name, as Description etc.) match the property names in your class.

Sunday Ironfoot
  • 12,840
  • 15
  • 75
  • 91
  • Doesn't help when I have 100+ potential columns/properties with any combination. Thanks anyway. – Harry May 24 '11 at 16:14
9

In addition to the example Tim gave you can do something like this:

IList selection =
    session.QueryOver<Cat>()
        .Select(
            c => c.Name,
            c => c.Age)
        .List<object[]>();

Above example was taken from: http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx

xhafan
  • 2,140
  • 1
  • 26
  • 26
Cole W
  • 15,123
  • 6
  • 51
  • 85
7

Use a ProjectionList to select the columns you want. See here for the examples.

Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
  • 11
    If the link dies, the answer is kinda useless. Include a small, but essential, snippet along with links ... –  Sep 17 '15 at 12:35