2

Hi: I tried looking at other posts. I apologize in advance if this is a duplicate post.

The sql query is something like this:

select * from ApiData where BrandID = 'SOUPBRAND' and Culture = 'de-de'
or 
( BrandID = 'SOUPBRAND' and Culture = 'pt-br' and EntityType = 'contactus')
or
(BrandID = 'examplefoods')

What would the nhibernate query look like?

This is what I have so far:

var mainQuery = _session.QueryOver<ApiData>();

            foreach (var brandPermission in permissions)
            {
                var query = _session.QueryOver<ApiData>();
                if (!string.IsNullOrEmpty(brandPermission.Culture) && !string.IsNullOrEmpty(brandPermission.Publication))
                {
                    query = query.Where(a => (a.Culture == brandPermission.Culture)
                                           && (a.Publication == brandPermission.Publication)); //FetchEntries(apiUser.DefaultCulture, apiUser.Publication);
                }
                else if (!string.IsNullOrEmpty(brandPermission.Culture))
                {
                    query = query.Where(a => (a.Culture == brandPermission.Culture)); //FetchEntries(apiUser.DefaultCulture);
                }

                query = query.Where(a => a.BrandId == brandPermission.BrandId);

//Here I would like to add
// mainQuery.AddORRestriction(query);
}

Just dont know how to achieve it. Saw a link :http://www.methodicmadness.com/2012/04/extending-queryover-with-or.html but didnt help.

Thanks!

1 Answer 1

6

thanks to: https://www.google.com/url?q=http://ayende.com/blog/4023/nhibernate-queries-examples&usd=2&usg=AFQjCNG43MDXKhjZ9LrCVSldeZuruBSdQQ and to: https://www.google.com/url?q=https://stackoverflow.com/questions/14478430/c-sharp-nhibernate-multiple-where-conditions-in-session-queryover&usd=2&usg=AFQjCNHRKbQz3EKGqOOh0gSV0LW2taSX-w

I got the following solution:

 var mainQuery = _session.QueryOver<ApiData>();

    var holdOr = Restrictions.Disjunction();
    foreach (var brandPermission in permissions)
    {
        var query = _session.QueryOver<ApiData>();
        var holdAnd = Restrictions.Conjunction();
        if (!string.IsNullOrEmpty(brandPermission.Culture) && !string.IsNullOrEmpty(brandPermission.Publication))
        {
            holdAnd.Add(Restrictions.Eq("Culture", brandPermission.Culture));
            holdAnd.Add(Restrictions.Eq("Publication", brandPermission.Publication));
        }
        else if (!string.IsNullOrEmpty(brandPermission.Culture))
        {
            holdAnd.Add(Restrictions.Eq("Culture", brandPermission.Culture));
        }

        holdAnd.Add(Restrictions.Eq("BrandId", brandPermission.BrandId));

        holdOr.Add(holdAnd);
    }
    mainQuery.Where(holdOr);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.