Question

Is it possible to do something like this in Linq to NHibernate - "Get all entities whose name starts with any string from the list".

I have this query:

var result=session.Query<Entity>()
                  .Where(e=> listOfStrings.Any(s=> e.Name.StartsWith(s)))
                  .ToList();

but I get

"NotSupportedException: Specified method is not supported."

Was it helpful?

Solution

Try this:

var query = session.QueryOver<Entity>();
var disjunction = new Disjunction();

foreach (var s in listOfStrings)
{
    disjunction.Add(Restrictions.On<Entity>(e => e.Name)
        .IsLike(s, MatchMode.Start));
}

var result = query.Where(disjunction).List();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top