Parameter names for Dapper multi-item Execute - dapper

With this code using Dapper .Execute:
using var c = new SqlConnection(ccstr);
var lst = new[] { 1, 2, 3 };
c.Execute("select #p", lst); // #p not recognized as parameter name
Is there a way to have a parameter name (here #p) for this native object list?

Leverage anonymous objects
using var c = new SqlConnection(ccstr);
var lst = new[] {
new {p = 1},
new {p = 2}
new {p = 3} };
c.Execute("select #p", lst);

Related

remove variables through an array

I'm trying to delete values of a lot of variables inside an array:
var fbUserID = String()
var fbUserName = String()
var meNickname = String()
var userIDOneSignal = String()
var deleteStrings = [fbUserID, fbUserName, meNickname, userIDOneSignal]
Is it possible to do something in line of this:
for i in deleteStrings {
i.removeAll()//remove all as in remove the values of each variable
}
I've also tried using deleteStrings[i].removeAll()
Due to value semantics you cannot mutate variables (as a pointer) from an array.
Rather than an array use a struct
struct User {
var fbUserID = "12"
var fbUserName = "Foo"
var meNickname = "Baz"
var userIDOneSignal = "123"
mutating func clear()
{
fbUserID = ""
fbUserName = ""
meNickname = ""
userIDOneSignal = ""
}
}
var user = User()
print(user.fbUserID) // "12"
user.clear()
user.fbUserID = ""
print(user.fbUserID) // ""
Simply delete the array elements
deleteStrings.removeAll()
If your class inherits from NSObject, you can use key value coding to access the fields by name:
class User: NSObject {
var fbUserID = String()
var fbUserName = String()
var meNickname = String()
var userIDOneSignal = String()
var deleteStrings = ["fbUserID", "fbUserName", "meNickname", "userIDOneSignal"]
func clearAll() {
deleteStrings.forEach { setValue("", forKey: $0) }
}
}
let user = User()
user.fbUserID = "12345"
user.fbUserName = "Joseph Doe"
user.meNickname = "Joe"
print(user.meNickname) // "Joe"
user.clearAll()
print(user.meNickname) // ""
If you want to delete/remove variable value then use optional otherwise your variable value only gets removed or become non-existence when it goes out of scope.
For Example:
var a: Int? = 5 // it have default value 5
a = nil // Now a have nil which is kind of telling that it contains nothing which is what you want to achieve
Now coming to your question.
Is it possible to do something in line of this:
for i in deleteStrings {
i.removeAll()
}
You are iterating over an array of strings and then for each string, you are trying to remove all the characters. First of all you will get error
error: cannot use mutating member on immutable value: 'i' is a 'let' constant
Even though you will correct is using var it will not achieve what you are trying to do i.e. I want to delete the variables value because still your fbUserID ... all other variables will have copies of the data you initialised with.
Now how to do it?
You can use optional to achieve it.
var fbUserID: String? = String()
var fbUserName: String? = String()
var meNickname: String? = String()
var userIDOneSignal: String? = String()
// To delete you will need to assign them nil
fbUserId = nil
Again, you can't do them over loop because var are of values type and when you add it to the list their copies get added.

How to get inner object value in localStorage getItem in angularJs?

I need to get the inner object value in localStorage.i.e object inside the object.
var filter = {
filterWord: null,
userId: null
}
filter.filterWord = listCAO.sortName;
filter.userId = listCAO.currentUser;
listCAO.filterBreadcumText.push(filter);
localStorage.setItem('entityBreadCumText', listCAO.filterBreadcumText);
LocalStorage only holds String pairs: 'string1'='string2'
So when you do localStorage.getItem('string1') it returns 'string2'.
If you want to store a Javascript Object, you need to convert it into a string first. JSON works best for that.
var myObj = [{'name': 'Paul', 'age': 22}, {'name': 'Steve', 'age': 68}];
myStr = JSON.stringify(myObj);
localStorage.setItem('myData', myStr);
Same when you read the data from localStorage
var myStr = localStorage.getItem('myData');
var myObj = JSON.parse(myStr);
var myName = myObj[0].name;
Or in one step
var myName = JSON.parse(localStorage.getItem('myData'))[0].name;
This may be another solution.
You can use it this way.
let obj = JSON.parse(localStorage.getItem('your_settings_name'));
let lobj: YourObject = <YourObject>obj;
If the data is stored as nested objects instead of an array as c14l 's answer, the syntax changes a little bit.
Let's store nested object first:
var myNestedObject = {"token": "Bearer", "profile": {"name":"Mustafa","expires_at":1678013824}};
myNestedStr = JSON.stringify(myNestedObject);
localStorage.setItem('myNestedData', myNestedStr);
Now let's see how to get the "name" from the nested object:
var nestedStr = localStorage.getItem('myNestedData');
var nestedObj = JSON.parse(nestedStr);
var nestedProfile = nestedObj.profile;
var nestedName = nestedProfile.name;
Or we can get "name" with a single line also:
var nestedNameWithOneLine = JSON.parse(localStorage.getItem('myNestedData')).profile.name;

Can Dapper handle dynamic column header

I have a stored procedure that always returns a list of strings. However, based on the parameters passed to the stored procedure, the column heading for the list of strings will change. Is Dapper able to handle this? If so, how should it be handled?
conn.Open();
var p = new DynamicParameters();
p.Add("Search_Function_CD", searchFunction, DbType.String, ParameterDirection.Input);
p.Add("Search_Value", searchValue, DbType.String, direction: ParameterDirection.Input);
p.Add("o_resultset", dbType: DbType.Object, direction: ParameterDirection.Output);
var Results = (IDictionary<string, object>)conn.Query(sql: CommonConstants.ProcedureConstants.PKG_GET_SEARCH_RESULTS, param: p, commandType: CommandType.StoredProcedure);
foreach (object o in Results)
{
string element = Results.Values.ElementAt(1) as string;
searchResults.Add(element);
}
conn.Close();
return searchResults;
You can get the value by a dynamic column name or index:
var row = (IDictionary<string, object>)con.Query("select * from Products").First();
string name = row["ProductName"]; // column name 'ProductName'
// or:
string name = row.Values.ElementAt(1) as string; // column index 1

Make 1 associative array out of 2 arrays in Actionscript 3

How do i make 1 array that has the keys of 1st array & its values are the values of the 2nd array in Actionscript 3.0?
Below is my [WRONG] code. Package import left out.
public class myPages extends Sprite {
protected var pageNames:Array = [];
protected var pageLayoutNo:Array = [];
private var pageLayoutNames:Object = new Object();
public function assignNamesLayouts {
//all the names of the pages
for(i=0; i<totalPages; i++) {
var pageMc:MovieClip=new MovieClip();
pageMc.name = menu.config.pages.page[i].#name;
pageNames[i]= pageMc.name;
}
//all layout numbers
for(i=0; i<totalPages; i++) {
pageLayoutNo[i] = menu.config.layoutNum.layNum[i];
}
setNames(pageNames);
setPLNo(pageLayoutNo);
setLayoutNames(pageLayoutNo,pageNames);
}
protected function setNames(a:Array) {
pageNames = a;
}
protected function setPLNo(a:Array) {
pageLayoutNo = a;
}
protected function setLayoutNames(a:Array, b:Array) {
var maps:Object = new Object();
maps.no = a;
maps.nm = b;
for each(var k:int in a) {
maps[k] = b;
}
}
}
Thank you.
I will agree with #Vesper. Closest solution to the associative array will be Dictionary. I also will add, some ideas for you. If your collection will have keys as Strings, you could use simple Object:
var keyName: String = "key1";
var anotherKey: String = "key2";
var collection: Object = {};
collection[keyName] = new MovieClip();
collection[anotherKey] = new Sprite();
trace(collection[keyName]);
If you want to use 2 arrays: one for keys, another one for values, you will need several functions(place object with key, get object by key, get object by value, remove object by key, etc.) to manage them, because indexes in arrays must be identical.
Dictionary is really what you want, but you could do it by creating an array of objects like this:
var newArray=combineArrays("name", arr1,"number",arr2);
function combineArrays(name1, arr1,name2, arr2){
var newArr=new Array();
for(var i<arr1){
var obj=new Object();
obj[name1]=arr1[i];
obj[name2]=arr2[i];
newArr.push(obj);
}
}
Then you can search on the object names or values for either object.

Compare Two Arrays, Get Uncommon Values

I have a simple problem that I'm having trouble thinking around:
var oldValues : Array = [ 4, 5, 6 ];
var newValues : Array = [ 3, 4, 6, 7 ];
I want to get the values from newValues that aren't in oldValues - 3, 7
I want to get the values from oldValues that aren't in newValues - 5
A way of getting both sets of values together would be nice as well - 3, 5, 7
I can only think of convoluted methods for each by using nested loops that do a lot of redundant checking. Can someone suggest something more clean? Thanks.
You need a bunch of loops, but you can optimize them and totally avoid nested loops by using a lookup object.
var oldValues : Array = [ 4, 5, 6 ];
var newValues : Array = [ 3, 4, 6, 7 ];
var oldNotInNew:Array = new Array();
var newNotInOld:Array = new Array();
var oldLookup:Object = new Object();
var i:int;
for each(i in oldValues) {
oldLookup[i] = true;
}
for each(i in newValues) {
if (oldLookup[i]) {
delete oldLookup[i];
}
else {
newNotInOld.push(i);
}
}
for(var k:String in oldLookup) {
oldNotInNew.push(parseInt(k));
}
trace("Old not in new: " + oldNotInNew);
trace("new not in old: " + newNotInOld);
Results:
Old not in new: 5
new not in old: 3,7
var difference : Array = new Array();
var i : int;
for (i = 0; i < newValues.length; i++)
if (oldValues.indexOf(newValues[i]) == -1)
difference.push(newValues[i])
trace(difference);
use casa lib
main page:http://casalib.org/
doc: http://as3.casalib.org/docs/
list class: http://as3.casalib.org/docs/org_casalib_collection_List.html
removeItems http://as3.casalib.org/docs/org_casalib_collection_List.html#removeItems
clone the list, and use newValues.removeItems(oldValues) to get the values from newValues that aren't in oldValue
and then use the same way to get the values from oldValues that aren't in newValue
concat the previous two results
There will be no looping in your code... Although there will be looping inside the code of list class :D

Resources