linq-to-nhibernate

Using NHibernate.Linq and getting 2 queries for a simple select, why?

Hi, so here's the code with irrelevant bits left out: public IEnumerable<T> GetByQuery(Expression<Func<T, bool>> filter { try { return Session.Linq<T>().Where(filter); } catch(Exception ex) { // custom exception handling here } finally { CloseSession(); } return null; } ...

NHibernate Linq - why does it use join when querying foreign key field?

Hi all I have a object model a bit like this: public class Foo { public int Id {get;set;} public string FooName {get;set;} } public class Bar { public int Id {get;set;} public Foo Foo {get;set;} } These correspond to tables in the database in the typical one-to-many kind of way. Using Linq to NHibernate to query Bars accord...

NHibernate: Object with id was not of the specified subclass: (loading object was of wrong class )

Hi, I am getting the error below: Object with id: 34dd93d3-374e-df11-9667-001aa03fa2c4 was not of the specified subclass: MyNamespace.ClassA (loading object was of wrong class [MyNamespace.ClassB1]) Call stack is: at NHibernate.Loader.Loader.InstanceAlreadyLoaded(IDataReader rs, Int32 i, IEntityPersister persister, EntityKey key, Obje...

How to set NHibernate Linq fetch strategy through a façade layer

I’m using NHibernate for data access, but accessing it through a façade layer. This layer consists of interfaces for the repositories, plus an IUnitOfWork interface which corresponds to the ISession object. In order that retrieved entities are managed correctly, repositories are passed an IUnitOfWork in their constructor and the IUnitOf...

Is there a way to combine results from two IQueryable<T> using NHibernate & Linq?

I have two separate queries that both return the same IQueryable, and I'd like to combine them prior to projection. It looks like neither Union or Concat are implemented in Linq to NHibernate? Does anyone know how I might go about achieving this? ...

Linq to Nhibernate - Compare 2 lists

I have 2 lists and I need to know if there are any matches. I've tried using request.Interests.Intersect(x.Post.Tags.Split(' ')).Count() > 0 but I get the error System.NotImplementedException : The method Intersect is not implemented. So, I tried a recursive function that returns a bool. And it's as if the function call is jus...

LINQ to NHibernate: selecting entity which has a specific entity in a one to many association

I would like to make this query: Session.Linq<User>().Where(u => u.Payments.Count(p => p.Date != null) > 0); In plain English I want to get all the users that has at least one payment with the date specified. When I run the sample code I get a System.ArgumentException with the message: System.ArgumentException : Could not find a ...

How to order by a field in Dictionary using Linq expression

I have an Entity class which has a Dictionary of fields called Data. Now I want to sort Entities by a field in Data. I tried somethings like - Entities.OrderBy(e => e.Data["UserId"]) and (e => e.Data.UserId) but both of these fail to create the right Criteria in NHibernate which should be something like {Data.UserId asc}. What is the ...

"Index was outside the bounds of the array." with LINQ to NHibernate

I am trying to retrieve a list of Items using LINQ to NHibernate, but it is a complex query involving multiple tables including a left join. I was originally using HQL, but I wanted to return an IQueryable so that I could dynamically add where clauses as needed based on user input. I am trying to populate a DTO that has some fields fro...

Query by type using Linq on an nHibernate table by sub-class structure.

Hi, I have quite a complex entity structure where several classes inherit from a base class, hence choosing a table per-subclass structure in nhibernate. BaseProject ProjectA : BaseProject ProjectB : BaseProject ProjectC : BaseProject ProjectD : BaseProject I want to search where one of the criteria will be ProjectType. I'm trying to...

searching using fluent nHibernate using LINQ where property may reside in numerious locations in derived classes

I have a class structure as follows (pseudo code): CompanyName (id, name) // Other information about the company IUsefulSearchField // A field that can appear at any depth in the below IUsefulSearchField2 // A field that can appear at any depth in the below BaseClass (id) // Some other values needed in class & This class will be inh...

Linq to Nhibernate using ANY

I have a requirement to query parent records if child records is one of given choice and i am unable to get this correctly the following works i.e if i get all the records and apply where clause on the results var linqSesssion = Session.Linq<PurchaseRequisition>(); linqSesssion.QueryOptions.RegisterCustomAction( c => c....

Can I get LINQ to NHibernate for 2.1.2GA

I'm using NHibernate 2.1.2 GA. We want to use LINQ for NHibernate with it, so I downloaded LINQ for NHibernate 1.0 (the one linked to in the first paragraph of Ayende's blog http://ayende.com/Blog/archive/2009/07/26/nhibernate-linq-1.0-released.aspx) - but it turns out that only works with NHibernate 2.1.0 - so no good to us. Every sear...

NHibernate Linq - how to create a where statement with IS NOT NULL

hi, how can i achieve this query with Nhibernate Linq? var l = session.CreateQuery("from Auswahl a where a.Returnkey is not null").List<Auswahl>(); i tried this but it always returns an empty list. var l = session.Linq<Auswahl>() .Where(item => !String.IsNullOrEmpty(item.Returnkey)) .Select(item ...

A different object with same identifier was already associated with the session error

Scenario: I have a customer object with lazy loading enabled. I use that throughout the program to call a list of customer for a listbox. It has relationships to the Division_Customer_Rel, Division_Email_Rel and Email_Address objects. All off the relationships have Lazy = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Inverse =...

NHibernate query mapped collection

Dear All! In my ASP.NET web-application I use NHibernate to persist my "User"-Instances, where each of them has a "Entries" - collection. It is a typical one-to-many mapping and it works just fine. The mapping-code for the entries looks like this: <bag name="Entries" cascade="all-delete-orphan"> <key column="UserID" /> <one-to-ma...

Duplicate Entries in NHIbernate Query.Expand (Left outer join)

Say I have two objects, ParentEntity and ChildEntity. When I do a var query = session.Linq<ParentEntity>(); query.Expand("ChildEntity"); understandably, it would cause duplicate entries to appear in ParentEntity. The way I've been handling this is like this. query.ToList().Distinct(); where I force a query using ToList and then f...

Linq to NHibernate issue

I currently have code which produces the following LINQ expression (taken from the WhoCanHelpMe showcase project). Its purpose is to bind together two expressions but I don't know if the following is actually a valid expression: .Where(p => (p.PostCodes .Any(pc =>(pc = value(PatchByPostCodeSpecification).postCode)) && In...

Linq to NHibernate projection to anon. type results in mystifying cast error

I have an TaxWork entity which is persisted using NHibernate. This entity has the following properties (among others): public virtual TaxWorkType Type { get; set; } //Kctc.TaxWorkType is an enumeration public virtual TaxWorkStatus Status { get; set; } //Kctc.TaxWorkStatus is an enumeration public virtual LegalWorkPriority Priority { get...

How can I get distinct values using Linq to NHibernate?

Hello, I've been trying to get distinct values using Linq to NHibernate and I'm failing miserably. I've tried: var query = from requesters in _session.Linq<Requesters>() orderby requesters.Requestor ascending select requesters; return query.Distinct(); As well as var query = from requesters in _session.Linq<Reques...