I'm writing an MVC application and want to query my database with a search parameter and put all of the results into a list. Right now my code to try this looks like:
Character character = db.Characters.ToList().Find(User.Identity.GetUserId());
Which is throwing up a error. Is there a way I can do this? Break it down into two statements for example? I tried
Character character = db.Characters.Find(User.Identity.GetUserId());
character = character.ToList();
but that isn't working either.
When using the extension method Find() you must be sure that in your model you have the attribute [Key] in the property representing your Id (primary key in your table).
And you can try like this:
Character character = db.Characters.Find(User.Identity.GetUserId());
The line above will work but not this one character= character.ToList(); because you declare character as an object and you can't assign it to a list of object in your case Character
If you want it to work, you can do something like this:
var myCharacters =db.Characters.Where(c=>c.someField=="someValue");
List<Character> myList = myCharacters.ToList();
Hope it will help.
Related
Hello guys i am trying to add Divisions to a game but there is a problem with tables, do you guys know how to solve this?
local divisonName = result3[i].name
print(divisonName)
ESX.Divisions[result3[i].owner].divisonName = {}
it's the code it should get division name and create a table with that like this (we assume divisonName gonna return swat for example):
["police"] = {
["swat"] = {
},
},
but instead of putting division name as SWAT it will put divisionName variable
i already print that divisionName variable and console return me SWAT so everything is fine with logic and value of variable but i guess there it's syntax issue i am not sure!
Console Debug image
Note that in Lua the construct some_table.field is a syntactic sugar for some_table["field"]. Whatever is written after the dot will be treated as a string key.
In your case, to index by the value stored in the variable, you need to write ESX.Divisions[result3[i].owner][divisonName], and not as .divisionName.
I have two tables in my rails app: properties and requests. Both tables have field called address. But in property address is always single value like Hollywood for example and in request table there could be complicated string like ["Hollywood", "Beverley Hills"]. My task is to get all properties which match by address. It means that if we have in request ["Hollywood", "Beverley Hills"] i need all properties that have address as Hollywood and all Beverley Hills. I tried something like this:
#properties = Property.where("address = ? ", #request.address)
and:
#properties = Property.where("address IN (?) ", #request.address)
but both variants don't work and i think because #request.address is actually string, not array.
So i would like if somebody would suggest me some good solution.
Your first try is wrong, as address in Property is a single value.
Your second try is correct but not the best.
You can use Property.where(address: #request.address). But you have to be sure that #request.address is an Array of String.
You shouldn't save an Array as a String like that: "[\"Hollywood\", \"Beverley Hills\"]. It is too hard to parse in the application. If you want to save this way, you will be better using serialize :address, Array in the model, because then it will return an Array when you try to access the attribute.
Anyway, check if #request.address in an Array of String, if not, parse it to be an Array of String.
You can just wrap it in an array
#properties = Property.where(address: [#request.address])
I have the following code:
dim key
for each key in Request.Querystring
'do something
key = sanitized_param(key)
next
My question for you classic-asp connoisseur, does classic-asp, or asp in general, pass the variables as references(memory), or by value? Trying to figure out if I sanitize the key variable and pass it back to itself, is it just "alive" for that loop, or does the new value get passed to the original QueryString?
Request.QueryString retrieves the query string parameters by value from the page headers.
You can only make changes to a query string once its been retrieved via Request.QueryString, but you can't make changes directly to Request.QueryString as it's read-only (If you could make changes you would presumably use Response.QueryString, but this isn't a valid response command).
I'm guessing you're trying to sanitize all your query strings in one go? This isn't really possible or indeed necessary. You would typically sanitize a query string as and when you request it:
Response.Write(sanitized_param(Request.QueryString("myQS")))
Or to assign the query string to a variable first then sanitize it:
Dim myQS
myQS = Request.QueryString("myQS")
myQS = sanitized_param(myQS)
' or
myQS = sanitized_param(Request.QueryString("myQS"))
Once the query string has been assigned to a variable and sanitized you're able to reference that variable as often as you like without having to pass it to your sanitize function again.
Also, your example code doesn't make much sense. The key value in your for each loop is referencing just the names of your query strings and not their values. If Response.QueryString was a valid ASP command you would do:
Response.QueryString(key) = sanitized_param(Request.QueryString(key))
But again, this isn't possible.
EDIT: This solution might be what you're looking for:
Create a dictionary object, call it "QueryString" for example. Loop through all your query strings and add a sanitized version to the dictionary object.
Dim QueryString : Set QueryString = Server.CreateObject("Scripting.Dictionary")
For Each Item In Request.QueryString
QueryString.Add Item,sanitized_param(Request.QueryString(Item))
next
Now, to retrieve a sanitized version of a query string just use:
QueryString.Item("query_string_name")
Or for the original unsanitized version you could still use:
Request.QueryString("query_string_name")
Just like Request.QueryString, the dictionary object is forgiving and won't return an error if you ask for a query string that doesn't exist.
You could also create a function for retrieving sanitized query strings, for example:
Function SanitizedQS(ByVal qsName)
SanitizedQS = sanitized_param(Request.QueryString(qsName))
End Function
And rather than using Request.QueryString("query_string_name") just use SanitizedQS("query_string_name").
I have been using parameters to query node indexes as such (using the rest api in java)-
final QueryResult<Map<String,Object>> result = engine.query("start nd=node:name_index(name={src}) return nd.age as age", MapUtil.map("src", "Susan");
However I haven't been able to get this to work for a collection of nodes/names. I have been trying something along the lines of-
final QueryResult<Map<String,Object>> result = engine.query("start nd=node:name_index(name={src}) return nd.age as age", MapUtil.map("src", Arrays.asList("Susan","Brian", "Ian"));
But it refuses to compile. I as wondering if there is something wrong in my syntax or that parameters are not designed to work in this context.
The name= syntax in the start is meant to do an index lookup on a property. It won't do an IN lookup. The way you can do this sort of lookup is like this (note it depends on Apache's StringUtils):
List<String> names = Arrays.asList("Susan","Brian", "Ian");
String luceneQuery = "name:("+StringUtils.join(names, ",")+")";
engine.query("start nd=node:name_index({luceneQuery}) return nd.age as age", MapUtil.map("luceneQuery", luceneQuery));
Just a note, this is the "legacy" index way of doing things. In 2.0 they've introduced label-based indexes, which work entirely differently.
Thanks a lot; though it would still only return a non empty answer when I added a space after the comma in line 2. I used-
String luceneQuery = "name:("+StringUtils.join(names, ", ")+")";
and it returned the age of one person. When I tried this:
String luceneQuery = "fs:(fs:"+ StringUtils.join(names, " OR fs:")+")";
it gave me all three ages. However, I am still unsure about whether this query will be able to leverage the usual advantages of parameters , i.e. will the engine be able to reuse the query and execution path the next time around (this time we may want to query for 4 names instead of 3)
i have some code that uses a string to determine dynamically what model class to use and for what field to find_by() on. however, i'm having a hard time with how to use these variables to get the model instance. specifically, i have
class Item
include MongoMapper::Document
key :my_variable, String
in my code i have
m = "Item"
f = "my_variable"
and i want to be able to
i = m.find_by_my_variable( f )
result = i[f]
any help is appreciated!
Since you're in Rails (judging by the category tag) you can use:
m = m.constantize
to make the string a constant, and then would something like this work for your query?
m.where("#{f} = ?", some_value)
(EDIT) or use send as ismaelga suggested, if you don't want an ActiveRecord::Relation array object
You could do eval(m.capitalize).send("find_by_#{variable_name}", variable_content)
Capitalize it's only to be sure it gets a capital letter.