How do you return specific properties from a linq query, not full objects?

I just downloaded the Linq provider for NHibernate, and I'm just a little excited. But I do not know the Linq syntax very well.

I can return whole objects from a query like this:

var query = from foo in session.Linq<Kctc.BusinessLayer.Domain.Case>()
                  where foo.CaseNumber > 0
                  select foo;

And I can select one property similar to this:

var query = from foo in session.Linq<Kctc.BusinessLayer.Domain.Case>()
                  where foo.CaseNumber > 0
                  select foo.Id;

But how to choose two properties, for example. foo.Id and foo.Bar? Or is it impossible?

thank

David

+5
source share
2 answers

Use anonymous projection:

var query = from foo in session.Linq<Kctc.BusinessLayer.Domain.Case>() 
              where foo.CaseNumber > 0 
              select new { foo.Id, foo.Bar }; 
+8
source

You need to create a new type Anonymousthat will only be available in the current scope (i.e., it cannot be returned from a method, etc.)

var query = from foo in session.Linq<Kctc.BusinessLayer.Domain.Case>() 
              where foo.CaseNumber > 0 
              select new { foo.Id, foo.Bar }; 

.

+1

All Articles