views:

110

answers:

1

I have a linq query that is passed into NHibernate which Linq2NHibernate will parse and return me populated entities.

string firstName = "Ryan";
Store store = _repository.Query<Store>().Where(x => x.Employees.Select(y => y.FirstName).Contains(firstName)).FirstOrDefault();

The troublesome part is x => x.Employees.Select(y => y.FirstName).Contains(firstName)

What this should be doing is selecting Stores that have Employees with the name Ryan.

I receive an error on the line above stating "Unhandled Expression Type: 1004"

It appears to me that it is a limitation of Linq2NHibernate and the .Select().Contains() just can't be parsed.

Any ideas? Has anyone else every received this error? What can I do to fix it or work around it?

[EDIT]

Here is what I ended up using instead.

string firstName = "Ryan"
Store store = _repository.Query<Store>().Where(x => x.Employees.Any(y => y.FirstName == firstName)).FirstOrDefault();

The Linq query being x => x.Employees.Any(y => y.FirstName == firstName)

+3  A: 

I don't use LINQ to NHibernate, but that's not how I'd write that query anywhere else. Try:

string firstName = "Ryan";
Store store = _repository.Query<Store>()
                         .Where(s => s.Employees.Any(
                                e => e.FirstName.Equals(
                                     firstName,  StringComparison.WhateverTypeYouNeed))
               ).FirstOrDefault();

If L2NH doesn't like string.Equals(), try ==

Craig Stuntz
I pretty much used what you gave me with a few minor tweaks. Thanks.
Ryan Montgomery