Nhibernate C# - Return only objects where string property is not null or empty - nhibernate

I'm writing a query using ICriteria that should return only the objects where property "Message" has value (i.e. is not null or empty). Below is the format I'm using.
ICriteria.Add(Restrictions.Not(Restrictions.Eq("Message", " "));
ICriteria.Add(Restrictions.Not(Restrictions.Eq("Message", String.Empty));
Can somebody help me with this? Thank you!

You might want something like:
ICriteria a = session.CreateCriteria(typeof(A));
a.add(Restrictions.Not(Restrictions.Eq("Message", " "));
a.add(Restrictions.Not(Restrictions.Eq("Message", string.Empty));
Although, your first one isn't really checking null, it's checking a single space.

I've not tried this, but it thnk the following should work:
ICriteria crit = session.CreateCriteria(typeof(theType))
.Add(Restrictions.IsNotNull("Message"))
.Add(Restrictions.IsNotEmpty("Message"));

Finally, I discovered the combination I was looking for!
lvCriteria.Add(Restrictions.Not(Expression.Eq("Msg", string.Empty)));
This combination of Restrictions and Expression works as expected; narrowing out all empty strings. I do not know why I could not achieve these results earlier even with:
lvCriteria.Add(Restrictions.Not(Restrictions.Eq("Msg", string.Empty)));
Thank you to all who tried.

What you really need is:
ICriteria crit = session.CreateCriteria(typeof(theType))
.Add(Restrictions.Not(Restrictions.Eq("Msg", string.Empty)))
.Add(Restrictions.IsNotNull("Msg"))
This is working fine for me and profiler shows correct SQL i.e. something like:
msg<>'' and msg is not null.
First answer did not work for me as Restrictions.IsNotEmpty/Empty applies only to collections (in NH 2.0.1 at least).

Related

Neo4j CYPHER node_auto_index for range lookup

I'd like to know if there's any way how to use auto indexing for range lookup. If I query something like
START age=node:node_auto_index(age<20 and age>10)
RETURN age;
it returns
Exception in thread "main" string literal or parameter expected.
I also tried something like
START age=node:node_auto_index(age = range(10,20))
RETURN age;
but it looks like it needs just age = "15" or something like that.
Any ideas what to do please?
The Lucene documentation states that the range syntax is as follows:
age:[10 TO 20]
The resulting query (but haven't tested this):
START age=node:node_auto_index("age:[10 TO 20]")
RETURN age;
The following read might also be interesting for you: Range queries in Neo4j using Lucene query syntax
EDIT: not sure if it will work for you; check out this github issue.
Why not to use where clause, something like below?
START node=node:node_auto_index(name='PersonName')
where node.age > 20 and node.age <20
RETURN node.age;
Following a workshop with Stefan Armbruster from Neo4J Technologies the same topic came up.
The most basic solution we came up with was to make sure that all the values stored for the numeric property has the same length. Thus, one will need to pad the age property values as follows, 011, 099, 103... And the values are stored as strings not numeric values.
Doing this everything should work from the index.

VB.net Strange Conditional Statement (IF)

Was wondering if someone could lend me their expertise. Pretty new to Vb.net and have come across this conditional statement in one of our products. Could someone please confirm the validity of the statement and explain what's going on here? I've tried numerous searches, but I cannot find anything related.
If (IsDBNull(dr("someID")), "0", dr("someID")) = someID.ToString() Then
I have changed the "id" value names as it's code from a commercial product, but the ID's used were all the same variable (ints).
Thanks for any input you can offer on this!
Joe
PS: The reason I can't check this at run time is because of how the product operates.
It is an inline If statement
If(condition,iftrue,iffalse) if condition is true evaluate and return iftrue else iffalse
The If operator in VB.NET 2008 acts as a ternary operator.[ REFERENCE]
Example:
Dim foo as String = If(bar = buz, cat, dog) 'Condition satisfied then it'll return cat else dog.
The statement is checking to see if the dr("SomeID") equals the value someID.ToString. The reason the If is required is because you need to check if the dr("someID") Is Null. If it is 0 is used instead which presumably should not be equal to someID.
It is the same as doing the following:
If Not IsDBNull(dr("someID")) Then
If dr("someID").ToString = someID.ToString Then
End If
End If
I would suggest that something like this would be more appropriate (checking integer values instead of comparing strings)
If(IsDBNull(dr("someID")), 0, CInt(dr("someID"))) = someID Then
I would also suggest Turning Option Strict On as the code you posted should not compile!

Linq: Checking if string column has a value (ie. is not null or empty)

Not sure if this is the best way to achieve this in Linq.
I'm trying to select Contact records in CRM 2011 where the EMailAddress1 contains a value. The following WHERE clauses I tried both caused exceptions:
Where c.EMailAddress1 > ""
Where Not String.IsNullOrEmpty(c.EMailAddress1)
So I ended up trying this, which seems to work ok:
Where Not c.EMailAddress1.Equals(String.Empty) _
And Not c.EMailAddress1.Equals(Nothing)
But I'm just not certain if this is the most efficient method.. it doesn't seem very elegant. Is there a neater way of checking if a string column has a value?
As said here Linq to CRM is really limited. That's why you can't use String.IsNullOrEmpty.
However, you should try to do this :
Where c.EMailAddress1 IsNot Nothing
When a field is empty, it's set to null in the database (never empty). This line should then be enough for your case.
Regards,
Kévin
just do :
Where c.EmailAddress <> ""

SQLiteQueryBuilder.buildQuery not using selectArgs?

Alright, I'm trying to query a sqlite database. I was trying to be good and use the query method of SQLiteDatabase and pass in the values in the selectArgs parameter to ensure everything got properly escaped, but it wouldn't work. I never got any rows returned (no errors, either).
I started getting curious about the SQL that this generated so I did some more poking around and found SQLiteQueryBuilder (and apparently Stack Overflow doesn't handle links with parentheses in them well, so I can't link to the anchor for the buildQuery method), which I assume uses the same logic to generate the SQL statement. I did this:
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(BarcodeDb.Barcodes.TABLE_NAME);
String sql = builder.buildQuery(new String[] { BarcodeDb.Barcodes.ID, BarcodeDb.Barcodes.TIMESTAMP, BarcodeDb.Barcodes.TYPE, BarcodeDb.Barcodes.VALUE },
"? = '?' AND ? = '?'",
new String[] { BarcodeDb.Barcodes.VALUE, barcode.getValue(), BarcodeDb.Barcodes.TYPE, barcode.getType()},
null, null, null, null);
Log.d(tag, "Query is: " + sql);
The SQL that gets logged at this point is:
SELECT _id, timestamp, type, value FROM barcodes WHERE (? = '?' AND ? = '?')
However, here's what the documentation for SQLiteQueryBuilder.buildQuery says about the selectAgs parameter:
You may include ?s in selection, which
will be replaced by the values from
selectionArgs, in order that they
appear in the selection.
...but it isn't working. Any ideas?
The doc for SQLiteQueryBuilder.buildQuery also says, "The values will be bound as Strings." This tells me that it is doing the straight-forward thing, which is writing the SQL leaving the ? parameter markers in place, which is what you are seeing, and binding the selectArgs as input parameters.
The ? are replaced by sqlite when it runs the query, not in the SQL string. The first string in the array will go where you see the first ?, and so on, when the query actually executes. I would expect the logged SQL to still have the ? markers.
Probably, your query fails because you are quoting the ?. For example, don't use WHERE ID = '?', just use WHERE ID = ?, and make sure the selectArgs is a string that satisfies the query.
Two things:
The ? substitution will not be done at this point, but only when the query is executed by the SQLiteDatabase.
From what I've seen, ? substitution only works for the right side of comparison clauses. For example, some people have tried to use ? for the table name, which blows up. I haven't seen anyone try using ? for the left side of the comparison clause, so it might work -- I'm just warning you that it might not.

Newbie issue with LINQ in vb.net

Here is the single line from one of my functions to test if any objects in my array have a given property with a matching value
Return ((From tag In DataCache.Tags Where (tag.FldTag = strtagname) Select tag).Count = 1)
WHERE....
DataCache.Tags is an array of custom objects
strtagname = "brazil"
and brazil is definitely a tag name stored within one of the custom objects in the array.
However the function continually returns false.
Can someone confirm to me that the above should or should not work.
and if it wont work can someone tell me the best way to test if any of the objects in the array contain a property with a specific value.
I suppose in summary I am looking for the equivalent of a SQL EXISTS statement.
Many thanks in hope.
Your code is currently checking whether the count is exactly one.
The equivalent of EXISTS in LINQ is Any. You want something like:
Return DataCache.Tags.Any(Function(tag) tag.FldTag = strtagname)
(Miraculously it looks like that syntax may be about right... it looks like the docs examples...)
Many Thanks for the response.
Your code did not work. Then I realised that I was comparing to an array value so it would be case sensitive.
However glad I asked the question, as I found a better way than mine.
Many thanks again !