NHibernate QueryOver with WhereRestriction as OR - asp.net-mvc

I have this query but I can't seem to find how I set my WhereRestrictionOn as an OR. Now they function as AND but I want one OR the other.
var privateInfo = Session.QueryOver<ConContact>()
.JoinAlias(c => c.PrivateInfos, () => pi)
.WhereRestrictionOn(c => c.FirstName).IsLike(_selectedFirstLetter + "%")
.WhereRestrictionOn(c => c.LastName).IsLike(_selectedFirstLetter + "%") // todo: change to firstname OR lastname
.Where(c => c.Status == ContactStatus.Approved)
.Select(
Projections.Property("pi.Id").WithAlias(() => sri.Id),
Projections.Property("FirstName").WithAlias(() => sri.Name), //todo: get fullname here => Add concontact object in privateinfo
Projections.Property("pi.Address").WithAlias(() => sri.Address),
Projections.Constant("Contact").WithAlias(() => sri.Type)
)
.TransformUsing(Transformers.AliasToBean<SearchResultInfo>())
.List<SearchResultInfo>()
.ToList();
Any help is much appreciated thx!
SOLUTION:
var privateInfo = Session.QueryOver<ConContact>()
.JoinAlias(c => c.PrivateInfos, () => pi)
.Where(
Restrictions.Disjunction()
.Add(Restrictions.Like("FirstName", _selectedFirstLetter + "%"))
.Add(Restrictions.Like("LastName", _selectedFirstLetter + "%"))
)
.Where(c => c.Status == ContactStatus.Approved)
.Select(
Projections.Property("pi.Id").WithAlias(() => sri.Id),
Projections.Property("FirstName").WithAlias(() => sri.Name), //todo: get fullname here => Add concontact object in privateinfo
Projections.Property("pi.Address").WithAlias(() => sri.Address),
Projections.Constant(NewObjectType.Contact).WithAlias(() => sri.Type)
)
.TransformUsing(Transformers.AliasToBean<SearchResultInfo>())
.List<SearchResultInfo>()
.ToList();

The top level .Where() family (including WhereRestrictionOn) is always joined with AND. So we have to explicitly use something like:
Restrictions.Or(restriction1, restriction1)
Restrictions.Disjunction().Add(restriction1).Add(restriction2).Add(...
So, this could be our case:
.Where(
Restrictions.Disjunction()
.Add(Restrictions.On<ConContact>(c => c.FirstName)
.IsLike(_selectedFirstLetter, MatchMode.Start))
.Add(Restrictions.On<ConContact>(c => c.LastName)
.IsLike(_selectedFirstLetter, MatchMode.Start))
// more OR ...
//.Add(Restrictions.On<ConContact>(c => c.MiddleName)
// .IsLike(_selectedFirstLetter, MatchMode.Start))
)
As discussed here: 16.2. Simple Expressions, for simple stuff we can even use || (cited small example):
.Where(p => p.Name == "test name" && (p.Age > 21 || p.HasCar))

Related

How to exclude record in elastic Search in ASP.Net MVC

I have to use the elastic search for the show the document.
I have written a query like below.
var result = elastic.Search<DocPosts>(s => s.Index(indexname).AllTypes().From(data.from).Size(data.PageSize).Query(q => q.Bool(b => b.Should(sh =>
sh.Match(mt1 => mt1.OnField(f => f.Text).Query(SQuery)) ||
sh.Match(mt2 => mt2.OnField(f => f.Title).Query(SQuery))
))).Highlight(h => h
.OnFields(f => f.OnField(d => d.DocText).PreTags("<mark>").PostTags("</mark>"), f => f.OnField(d => d.Title).PreTags("<mark>").PostTags("</mark>"))
).Filter(f => f.Term(t => t.Id, Id)));
this is working perfectly.
in this, I want to exclude some record that has come in below query.
var Proc = (from p in db.Pro where p.ParId == ParId && (u.UserId != temp.UserId || u.UserId == null)
select p.procID).ToList();
how can I perform this filter or exclude from elastic search?

ZF2 - Form creation missing validators

I am having a little trouble adding validators to a ZF2 form object. I an building a form from a schema set in a database so I can validate a set of user input quickly.
This is the code that generates the form object
//initialise the form
$form = new Form();
//need to loop through the schema to create the form
for($i=0; $i < count($schema); $i++)
{
$form_options = array();
//add the basics to the form
$form_options['name'] = $schema[$i]['field_name'];
$form_options['type'] = $schema[$i]['field_type'];
//check if this is a required field
if($schema[$i]['is_required'] == 'true')
{
$form_options['required'] = true;
}
//add functions to filter the input form
$function_filters = explode(',', $schema[$i]['function_filter']);
if(!empty($function_filters))
{
$filters = array();
for($j=0; $j < count($function_filters); $j++)
{
$filters = array('name' => $function_filters[$j]);
}
$form_options['filters'] = $filters;
}
//add validators to the field array
$validators = array();
if(!is_null($schema[$i]['min_length']) && !is_null($schema[$i]['max_length']))
{
$validators[] = array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => (int) $schema[$i]['min_length'],
'max' => (int) $schema[$i]['max_length'],
)
);
}
//our regex validator if it exists
if(!is_null($schema[$i]['regex_filter']) || strlen($schema[$i]['regex_filter']) != 0)
{
$validators[] = array(
'name' => 'regex',
'options' => array(
'pattern' => $schema[$i]['regex_filter'],
'messages' => array(
\Zend\Validator\Regex::INVALID => $schema[$i]['regex_invalid'],
\Zend\Validator\Regex::NOT_MATCH => $schema[$i]['regex_not_match'],
\Zend\Validator\Regex::ERROROUS => $schema[$i]['regex_errorus'],
)
)
);
}
//only add the validators if theres something in there
if(!empty($validators))
{
$form_options['validators'] = $validators;
}
$form->add($form_options);
}
//return our form object
return $form;
The block of code behaves the way I expect it, the output of $form_option after it's performed the above code looks like this:-
Array
(
[name] => username
[type] => Text
[required] => 1
[filters] => Array
(
[name] => StripTags
)
[validators] => Array
(
[0] => Array
(
[name] => StringLength
[options] => Array
(
[encoding] => UTF-8
[min] => 3
[max] => 50
)
)
[1] => Array
(
[name] => regex
[options] => Array
(
[pattern] => /^[a-zA-Z0-9_]{3,50}$/
[messages] => Array
(
[regexInvalid] => L_REGEX_INVALID
[regexNotMatch] => L_REGEX_USERNAME
[regexErrorous] => L_REGEX_ERRORUS
)
)
)
)
)
When I came to test it, it ignores the StringLength & Regex validators entirely, and only pay attention to the required validator for the last form element.
Anyone have any idea what's gone wrong?

NHibernate QueryOver where ID is in a list

I have a webpage with criteria fields which filters data. The query for this is:
CompanyAddress ca = null;
CompanyAddress cad = null;
WorkInfo wi = null;
PrivateInfo pi = null;
SearchResultInfo sri = null;
/***************************/
/* SEARCH FOR MAIN COMPANY */
/***************************/
var company = Session.QueryOver<Company>()
.JoinAlias(c => c.Addresses, () => ca)
.Where(() => ca.Main)
.Where(c => c.Status == ContactStatus.Approved)
.Select(
Projections.Property("Id").WithAlias(() => sri.TopId),
Projections.Property("ca.Id").WithAlias(() => sri.Id),
Projections.Property("Name").WithAlias(() => sri.Name),
Projections.Property("ca.Address").WithAlias(() => sri.Address),
Projections.Property("ca.ContactData").WithAlias(() => sri.ContactData),
Projections.Constant(ContactClassType.Company).WithAlias(() => sri.Type)
);
if (!string.IsNullOrEmpty(_name)) company = company.WhereRestrictionOn(c => c.Name).IsLike("%" + _name + "%");
//// TODO: fix error
if (_selectedTag != null) company = company.Where(Restrictions.In("_selectedTag", ca.Tags.Select(x => x.Tag.Id).ToArray()));
if (!string.IsNullOrEmpty(_selectedCity)) company = company.Where(() => ca.Address.City == _selectedCity);
if (_selectedCountry != null) company = company.Where(() => ca.Address.Country.Id == _selectedCountry);
if (!string.IsNullOrEmpty(_selectedZipCode)) company = company.Where(() => ca.Address.ZipCode == _selectedZipCode);
company.TransformUsing(Transformers.AliasToBean<SearchResultInfo>());
Now if selectedTag has an ID the query gives me an error telling ca is null. All the other where clauses work except this one. So I want to check if the incoming ID is in the Tag list from the object CompanyAddress.
Does anyone have an idea whats wrong here?
** SQL FROM ANDREW WITHTAKERS SOLUTION **
SELECT this_.Id as y0_,
ca1_.Id as y1_,
this_.Name as y2_,
ca1_.IdAddressType as y3_,
ca1_.Street as y4_,
ca1_.Number as y5_,
ca1_.ZipCode as y6_,
ca1_.City as y7_,
ca1_.Country as y8_,
ca1_.Email as y9_,
ca1_.Fax as y10_,
ca1_.PhoneNumber as y11_,
ca1_.CellNumber as y12_,
ca1_.Url as y13_,
#p0 as y14_
FROM CON_Company this_ inner join CON_CompanyAddress ca1_ on this_.Id=ca1_.IdCompany
inner join CON_CompanyAddrTag tagalias2_ on ca1_.Id=tagalias2_.IdCompanyAddr
WHERE ca1_.Main = #p1 and this_.Status = #p2 and tagalias2_.Id = #p3',N'#p0 int,#p1 bit,#p2 int,#p3 int',#p0=3,#p1=1,#p2=0,#p3=3
You're going to have to join to Tag in order to do this:
if (_selectedTag != null)
{
Tag tagAlias = null;
company
.JoinAlias(() => ca.Tags, () => tagAlias)
.Where(() => tagAlias.Id == _selectedTag.Id)
}
(Assuming you want Tags compared by Id)
The thing to remember about QueryOver/Criteria is that it's ultimately going to be turned into SQL. calling ca.Tags.Select(...) doesn't make sense from inside of a SQL query since there's an implied JOIN there to Tag.
Also, you're mixing QueryOver and Criteria syntax which is kind of confusing. I'd rework things a bit:
Company companyAlias = null;
var company = Session.QueryOver<Company>(() => companyAlias)
.JoinAlias(c => c.Addresses, () => ca)
.Where(() => ca.Main)
.Where(c => c.Status == ContactStatus.Approved)
.Select(
Projections.Property(() => companyAlias.Id).WithAlias(() => sri.TopId),
Projections.Property(() => ca.Id).WithAlias(() => sri.Id),
Projections.Property(() => companyAlias.Name).WithAlias(() => sri.Name),
Projections.Property(() => ca.Address).WithAlias(() => sri.Address),
Projections.Property(() => ca.ContactData).WithAlias(() => sri.ContactData),
Projections.Constant(ContactClassType.Company).WithAlias(() => sri.Type)
);

NULL value in :conditions =>

Contract.all(:conditions => ['voided == ?', 0]).size
=> 364
Contract.all(:conditions => ['voided != ?', 0]).size
=> 8
Contract.all.size
=> 441
the 3 numbers does not added up (364 + 8 != 441). What's the proper way write the :conditions to count the rows which the voided column value is NULL or equal to zero?
Contract.all(:conditions => {:voided => nil})
or
Contract.all(:conditions => ['voided IS NULL'])
Contract.all(:conditions => ["voided is ?", nil]).size
Contract.all(:conditions => ["voided is not ?", nil]).size

rails active record - complicated conditions clause

this works:
ids = [1,2]
varietals = Varietal.find(:all, :conditions => [ "id IN (?)",ids])
But what I want to do is that plus have a condition of: deleted => false
varietals = Varietal.find(:all, :conditions =>{ :deleted => false})
any ideas?
am i going to have to use find_by_sql?
I would handle this with a named_scope to communicate intent and foster re-use:
named_scope :undeleted,
:conditions => { :deleted => false }
Then you can simply use:
varietals = Varietal.undeleted.find([1,2])
You can do it a few ways, but this is the most straight forward:
varietals = Varietal.find( [1,2], :conditions => { :deleted => false })
You can see in the docs that the first parameter of find can take an integer or an array.
ids = [1,2]
varietals = Varietal.find(:all, :conditions => {:id => ids, :deleted => false})
This should work, haven't tested it though.
From the docs:
An array may be used in the hash to
use the SQL IN operator:
Student.find(:all, :conditions => { :grade => [9,11,12] })

Resources