How to use Any operator in NHibernate to select by two parameters?

I have a UserObject that contains ID, GUID, Name and Age. My function gets a list of objects the contains only ID and GUID, and need to return the UserObject(s) the matches the params. For example:

userIds = new [] {
    { ID = 1, GUID = "AAAAAA" },
    { ID = 2, GUID = "AAAAAB" },
};

var result = from user in session.Query<UserObject>()
             where userIds.Any(u => u.ID == user.ID && u.GUID == user.GUID)
             select user;

The query will return the UserObjects only if the ID exists and the GUID matches. The problem is that NHibernate does not support the Any method. How can this be achieved?

Answers


I believe NHiberante does not support the Any operator on an unmapped array (userIds).

You can try to use,

var result = session.Query<UserObject>()
                    .Where(user => userIds.Select(u => u.ID).Contains(user.ID))
                    .Where(user => userIds.Select(u => u.GUID).Contains(user.GUID))
                    .SingleOrDefault();

As shown in this related answer.


How about looping over the passed in objects, adding disjuctions as you go along for each record.

var c = session.CreateCriteria<User>();
var disjunction = new Disjunction();
foreach (var ids in userIds)
{
    var q = new Conjunction().Add(Restrictions.Eq("userId", ids.userId))
            .Add(Restrictions.Eq("GUID", ids.GUID));
    disjunction.Add(q); 
}
c.Add(disjunction);

Sorry if the code does not compile just as written and is not at all tested as I am away from VS right now. The code can also probably be written more concisely. What the code is effectively doing is generating a query of the type "Select * from Users where ((userId = userId1 and GUID = Guid1) or(userId = userId2 and GUID = GUID2) or....)" Hope this is what you were looking for.


Need Your Help

CSS on:hover changing childs attributes

css html hover parent-child

so i was wondering if this where possible.

Pop up For Alert

c# java .net powershell vbscript

I'm going to ask the end users to clean their cache addresses using

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.