1

I am working on NET MVC 3.0 and Nhibernate 3.0. I want to fetch only one property from database to an object.

For instance, suppose I have a class Module. I want to select all the names from module table (like select modulename from module query) and an prepare a list of module objects which have only name. All other properties can be null.

I tried this using QueryOver API:

IQueryOver<ProjectModule> module = session.QueryOver<ProjectModule>()
    .Select(a=>a.Name)                   
    .TransformUsing(
        NHibernate.Transform.Transformers.AliasToBean<ProjectModule>());

pm = module.List<ProjectModule>();

pm is IList<ProjectModule> type.

Transaction gets committed successfully. No error occurred, but I get a list of module objects with all properties = null. Module name null, module id null etc.

I checked what query is executing on SQL using NUnit and got this: SELECT this_Name as y0_ FROM ProjectModule this_.

3 Answers 3

1

To be more accurate create a DTO object, assume ProjectModuleDto, that will contain only the Name. It's not a good practice to use the the domain object with uninitialized values through your code, cause it creates confusions of filled data in various scenarious.

And the fllowing code will do the trick - populate the list of DTOs: ProjectModuleDto with correct Name of project module from database:

ProjectModuleDto projectModuleDto = null;           
IQueryOver<ProjectModule> module = session.QueryOver<ProjectModule>()
    .SelectList(
        list=>list.Select(a => a.Name).WithAlias(() => projectModuleDto.Name)
    )                 
    TransformUsing(NHibernate.Transform.Transformers.AliasToBean<ProjectModuleDto>());

pm = module.List<ProjectModuleDto>();           
Sign up to request clarification or add additional context in comments.

3 Comments

this works :) thanks. but as i am an student can you please tell me few thinks 1.Why we have to use SelectList(list=>list...) Why not just select(a=>a.Name)? As i am trying to do 2. Why we have uesed '()' in WithAlias what is meaning of that?
if you want to use only (a=>a.Name) than you'll have to go with the answer that is provided by Miroslav Popovic, but in this case you'll get a list of strings, and not a list of types objects, that also may contain multiple properties. Assume your ProjectModuleDto has Name and Color, than you just write .SelectList( list=>list.Select(a => a.Name).WithAlias(() => projectModuleDto.Name).Select(a => a.Color).WithAlias(() => projectModuleDto.Color) ) and you are done.
Thnaks its clear to me now. what about 2. Why we have uesed '()' in WithAlias what is meaning of that?
1

If you are fetching only a single property, you don't need to use transformers. Try to use a List<string> directly:

var moduleNames = session.QueryOver<ProjectModule>()
    .Select(a => a.Name)                   
    .List<string>();

Read more about QueryOver syntax on NHibernate blog.

3 Comments

i don't want result in string list.i want in IList<ProjectModule>.
It's fairly easy to initialize a list of ProjectModules from a list of string, although I would rather go with diadiora's suggestion and create a separate DTO for that.
Thanks for reply it's also correct solution but as i need result in ProjectModule object so i go with diadiora's Answer.Thnak you Very Much.
0

Is this what you're looking for?

  List<ProjectModule> result = new List<ProjectModule>();

  session.QueryOver<ProjectModule>()
  .Select(a => a.Name)                   
  .ToList().ForEach(delegate(string mName)
  {
       result.Add(ProjectModule() { Name = mName });
  });

1 Comment

Hey did you seen diadiora's solution which one is better urs or diadiora's?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.