Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you return certain properties from a linq query, rather than complete objects?

I've just downloaded the Linq provider for NHibernate and am just a little excited. But I don't know Linq syntax that 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 a single property like this:

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

But how would I select two properties, e.g. foo.Id and foo.Bar? Or is that not possible?

Thanks

David

like image 942
David Avatar asked Jul 15 '10 13:07

David


1 Answers

Use an anonymous projection:

var query = from foo in session.Linq<Kctc.BusinessLayer.Domain.Case>() 
              where foo.CaseNumber > 0 
              select new { foo.Id, foo.Bar }; 
like image 55
Stephen Cleary Avatar answered Sep 29 '22 23:09

Stephen Cleary