Can I pass a variable to a Google query's where condition? - google-api

I think this is a simple question, but need help as I am new...so please go easy on me =)
I want to use a variable in the WHERE clause of my query using the Google Visualization API Query Language.
I have a variable called studentId that the user can set using a textbox on the page, and would like to run this query query.setQuery('SELECT B WHERE B = studentId COUNT(B)'); but this query is only checking cells where the actual contents of the cell is "studentId".
Can I pass a variable's value to the where clause of this query? How?
I feel that there should be a simple answer to this but am coming up blank after much searching. Thanks for looking.

Yes, you can use a variable in the query. If studentId is a number, use this:
query.setQuery('SELECT B WHERE B = ' + studentId + ' COUNT(B)');
if it is a string, use this:
query.setQuery('SELECT B WHERE B = "' + studentId + '" COUNT(B)');

Related

In cmdb_rel_ci table I want to retrieve all the unique parent.sys_class_name with count for Type Depends on::Used by

In the cmdb_rel_ci table, I want to retrieve the value and total count of all the unique parent.sys_class_name values for Type(cmdb_rel_type) "Depends on::Used by".
I was trying to use with GlideAggregate, but classname is showing empty.
Can anyone offer some advice?
Use GlideAggregate on the cmdb_ci table, not cmdb_rel_ci. The rel table has no sys_class_name if I recall.
You could also try dot-walking to it using GlideRecord, or using a Join query.
I don't know what "for type Depends on::Used by" means. Pretty sure it doesn't mean anything.
Please take the time to explain your questions clearly and include your code next time if you're expecting anyone to take time out of their day to help you.
As suggested already, use Glideaggregate (more info here)
Try this as an example:
var ga = new GlideAggregate('cmdb_rel_ci');
ga.addQuery('type.name', 'STARTSWITH', 'Depends on::Used by');
ga.addAggregate('COUNT', 'parent');
ga.query();
while (ga.next()) {
var parent = ga.parent.getDisplayValue();
var parentCount = ga.getAggregate('COUNT', 'parent');
gs.info('CI ' + parent + ' has ' + parentCount + ' relationships with type Depends on::Used by');
}

Retrieve Umbraco Tags datatype field in a LINQ query

I want to insert the values of an Umbraco.Tags datatype field into my razor output for each record from a LINQ query of my content. I'm having trouble finding the right syntax. I realize it should be a string array but everything I have tried fails.
In the page for the individual content I have been able to use the following where the content field is name "Tags".
var tags = CurrentPage.Tags;
The LINQ query is complex, but I am simply trying to add the tags to the output so that I can search inside the page for specific records on the client side. I have removed most of the query to make it easier to read.
var products = Model.Content.Site().Descendants<PenList>().First().Children("penProduct")
.Where(x => x.IsVisible())
.OfType<PenProduct>()
.Select(x => new ProductSearchResultItem() {
FilterText = x.ProductName + " " + x.ProductDescription.ToString() + " " + x.CompanyName.NullSafeToString() + " " + x.ProductType.ToString()
+ " " + (x.Parent.GetProperty("tags") != null ? x.Parent.GetProperty("tags").Value : string.Empty)})
I have also tried just getting x.Tags or x.GetProperty("tags") with no success. Any suggestions would be appreciated.
Is the tags property on the penProduct doc type or the penList? It looks like you're looking for it on the penList.
But I would think it would behave like any other string. I don't normally use typed modals, but to get a list of the tag values in my views I use:
var tags = Model.Content.GetPropertyValue<string>("tags", "").Split(',');
Since the field is actually a string array I had to use the following code to get the property as a string array generic otherwise it would return the text of the type "System.String[]".
x.GetPropertyValue<string[]>("tags")

Finding Last Name Using LINQ

I am trying to get last name using linq in visual studio. In my database, i have the Field name like "FullName".
In this Field I have value like "Subbu Cargos"
I Wanna Display the "Cargos" in my textbox.
How can i make a simple linq query?
Would it be over simple to say:
return FullName.Split(' ').Last()
?
I would suggest breaking it up into different fields - Firstname, Middlename, lastname, Title - and rebuilding the name on the fly when you come to display it.
If you're still determined to use one field, then consider a query like:
string s = "Subba Cargos";
var lastnames = from name in s.Split(new Char[] { ' ' }).Last<string>()
select name;
I would suggest not trying to parse out the last name. Like you say, first and last names could be switched around, someone might have a second name, or a last name that consists of multiple words (“van Dijk”), or may not have entered a last name at all.
Check out this article: Falsehoods Programmers Believe About Names
If you still want to do this however, try something like this:
customers.Select(c => c.FullName.Split(' ').Last());
You might not be able to this on the server side. In that case:
customers
.Select(c => c.FullName)
.ToList()
.Select(n => n.Split(' ').Last());
Untested, but this should give a rough idea.
You could also do it like this:
customers
.Select (b => b.FullName.Substring ((b.FullName.IndexOf(' ') + 1)));

Using variables instead of objects

I used this line
.RecordSource = "select * from tblpersonal where empid like '" & Me.lblIDNumber.Caption & "*'"
...my question is, what if I use a variable (varIDNumber) instead of object (lblIDNumber), what would be the syntax? I am using VB6.0
You didn't mention txtIDNumber in the code -- you mentioned lblIDNumber. I assume you mean for those two to be the same. In other words, the code you have at present should be something like this:
.RecordSource = "select * from tblpersonal where empid like '" & Me.txtIDNumber.Text & "*'"
So you are using the value of a text box in a form to populate the SQL query. Am I right so far?
And you are asking, what if I store the ID number in a variable rather than a text field? I agree, this is probably a step in the right direction.
So you might create a variable in the "General Declarations" section of the form using:
Dim idNumber As Integer
With the idea being to update the value of that variable each time the text field changes. Note: I am assuming that the "ID number" is an integer -- if not, you should use a String instead.
Now you need to update that variable when the text field changes. In the txtIDNumber_Change event, you will want to add code to convert the string txtIDNumber.Text into an Integer, and store it in idNumber. I forget the exact syntax, but I am guessing something like:
idNumber = Int(txtIDNumber.Text)
Finally, you can now use the idNumber variable in the SQL query rather than the text box:
.RecordSource = "select * from tblpersonal where empid like '" & idNumber & "*'"
Replace Me.lblIDNumber.Caption with varIDNumber
If you have a constant in the label and would prefer for the constant to be stored in a variable instead, create a Const in the form's code. Let's say the label has the text "43" in it.
In the general declarations section of the form, add the code:
Const idNumber As Integer = 43
Then, when constructing the query:
.RecordSource = "select * from tblpersonal where empid like '" & idNumber & "*'"
This will construct the query using the constant 43. I don't really see the point of this -- if you want the employee number to be something the user can type in, see my other answer.

Linq To NHibernate: .StartsWith on multiple properties

I'm trying to accomplish the following query (notice .StartsWith):
return (from p in _session.Linq<Profile>()
where (p.Firstname + " " + p.Lastname).StartsWith(wildcard)
select p).ToList();
This throws: could not resolve property: Firstname.Lastname.
If I do this:
return (from p in _session.Linq<Profile>()
where p.Firstname.StartsWith(wildcard)
select p).ToList();
Everything is working. How can this be?
Thanks in advance!
The Where Expression does not know how to handle the concatenation of strings. It is trying to make sense of the properties, not the values.
Also, for future reference the StartsWith with the concat and the other one with out would in practice return the same thing.
Is this what you want?
return (from p in _session.Linq<Profile>()
where p.Firstname.StartsWith(wildcard) || p.Lastname.StartsWith(wildcard)
select p).ToList();
Update: rewritten answer based on new insights and edited questions.
What's in wildcard and what's the expected output vs. input? If you concat "Abel" + " " + "Braaksma" it will return true for wildcard.StartsWith("Abel") or wildcard.StartsWith("Abel Br") but not wildcard.StartsWith("Braaks"). Do you perhaps mean Contains instead? But this won't solve your error:
The exception you receive seems to come from NHibernate, not from your code. Is it possible that Lastname does not have a correct mapping to the database table? Can you show the stacktrace? Can you access the properties outside of the context of the LINQ statement, but filled with data from the table?

Resources