Handling varying number of result sets in Petapoco or Dapper? - dapper

I have used QueryMultiple before for handling multiple result sets, but I knew the exact number of result sets being returned. In this case, when I call a stored proc with QueryMultiple, the number of result sets returned varies. Is it possible to handle this in PetaPoco or another orm such as Dapper?

Dapper's QueryMultiple method returns a GridReader; a GridReader has a .IsConsumed property that should change to true when you've read all the available result sets, so that might work:
using(var reader = conn.QueryMultiple(...)) {
do {
var data = reader.Read<...>().AsList();
// ...
} while(!reader.IsConsumed);
Alternatively, Dapper has an ExecuteReader method that just does the "pack the parameters and invoke step", and a GetTypeDeserializer method that exposes just the "materialize a row into an object" code, so you could manually combine those, i.e.:
using(var reader = conn.ExecuteReader(...)) {
do {
var parser = SqlMapper.GetTypeDeserializer(...);
while(reader.Read()) {
var obj = parser(reader);
// ...
} while(reader.NextResult());
(I don't know much about petapoco, sorry)


Using setvalues with a for loop

I am currently trying to perform a "copyTo" or "setValues" action.
I need to copy one grid (Sheet1!J16:Q36) to another grid (Sheet2!J16:Q36)
But not every cell shall be copied. Only those values that are not identical to the Sheet1 values shall be copied.
I have tried the below code with success, but sadly the script takes ages.
I understand that a batch operation with getValues in an array will be quicker, but I lack the capability to do that script.
I also used a third grid which compared the values of sheet1 and 2 and returned 1 or 0. Only if the value 1 was shown, the cell was considered by the for loop. I take it that this is inefficient.
Thank you for your help. I appreciate it a lot.
var ratenprogramm = SpreadsheetApp.getActiveSpreadsheet();
var ratenprogrammmain = ratenprogramm.getSheetByName("Ratenprogramm");
var vorlageratenprogramm =
if(vorlageratenprogramm.getRange(37+i,9+j).getValue() == 1)
vorlageratenprogramm.getRange(15+i,9+j).copyTo(ratenprogrammmain.getRange(15+i,9+j),{contentsOnly: true});
As you have noticed, calling any external services, including methods
like getValue() make your script slow, see Apps Script Best
Your code can be optimized by replacing the multiple getValue()
requests by a single getValues().
Within the nested loops you can specify a multiple amount of ranges
and values that can be written with the Advanced Sheets Service,
with the Sheets API method spreadsheets.values.batchUpdate into
the corresponding ranges of the destination sheet, see also
function myFunction() {
var ratenprogramm = SpreadsheetApp.getActiveSpreadsheet();
var ratenprogrammmain = ratenprogramm.getSheetByName("Ratenprogramm");
var vorlageratenprogramm = ratenprogramm.getSheetByName("VorlageRatenprogramm");
var data=[];
var range=vorlageratenprogramm.getRange(15,9,21,8);
var values=range.getValues();
if(values[i+1][j] == 1)
var cell=range.getCell(i+1,j+1).getA1Notation();
data.push([{ range:'Ratenprogramm!'+ cell, values: [[values[i+1][j]]]}]);
var resource = {
valueInputOption: "USER_ENTERED",
data: data
Sheets.Spreadsheets.Values.batchUpdate(resource, spreadsheetId);
Keep in mind that if you have many different ranges, it might be
easier and faster to overwrite the sheet with the complete range,
rather than using nesting looping. E.g.

String Jsonarray in android [duplicate]

So, I get some JSON values from the server but I don't know if there will be a particular field or not.
So like:
{ "regatta_name":"ProbaRegatta",
And sometimes, there will be an extra field like:
{ "regatta_name":"ProbaRegatta",
I would like to check if the field named "club" exists so that at parsing I won't get
org.json.JSONException: No value for club
JSONObject class has a method named "has":
Returns true if this object has a mapping for name. The mapping may be NULL.
You can check this way where 'HAS' - Returns true if this object has a mapping for name. The mapping may be NULL.
if (json.has("status")) {
String status = json.getString("status"));
if (json.has("club")) {
String club = json.getString("club"));
You can also check using 'isNull' - Returns true if this object has no
mapping for name or if it has a mapping whose value is NULL.
if (!json.isNull("club"))
String club = json.getString("club"));
you could JSONObject#has, providing the key as input and check if the method returns true or false. You could also
use optString instead of getString:
Returns the value mapped by name if it exists, coercing it if
necessary. Returns the empty string if no such mapping exists
just before read key check it like before read
JSONObject json_obj=new JSONObject(yourjsonstr);
//it's contain value to be read operation
//it's not contain key club or isnull so do this operation here
isNull function definition
Returns true if this object has no mapping for name or
if it has a mapping whose value is NULL.
official documentation below link for isNull function
You can use has
public boolean has(String key)
Determine if the JSONObject contains a specific key.
JSONObject JsonObj = new JSONObject(Your_API_STRING); //JSONObject is an unordered collection of name/value pairs
if (JsonObj.has("address")) {
//Checking address Key Present or not
String get_address = JsonObj .getString("address"); // Present Key
else {
//Do Your Staff
A better way, instead of using a conditional like:
if (json.has("club")) {
String club = json.getString("club"));
is to simply use the existing method optString(), like this:
String club = json.optString("club);
the optString("key") method will return an empty String if the key does not exist and won't, therefore, throw you an exception.
Try this:
let json=yourJson
Json has a method called containsKey().
You can use it to check if a certain key is contained in the Json set.
File jsonInputFile = new File("jsonFile.json");
InputStream is = new FileInputStream(jsonInputFile);
JsonReader reader = Json.createReader(is);
JsonObject frameObj = reader.readObject();
if frameObj.containsKey("person") {
//Do stuff
Try this
I used hasOwnProperty('club')
var myobj = { "regatta_name":"ProbaRegatta",
if ( myobj.hasOwnProperty("club"))
// do something with club (will be false with above data)
var data = myobj.club;
if ( myobj.hasOwnProperty("status"))
// do something with the status field. (will be true with above ..)
var data = myobj.status;
works in all current browsers.
You can try this to check wether the key exists or not:
JSONObject object = new JSONObject(jsonfile);
if (object.containskey("key")) {
//etc. etc.
I am just adding another thing, In case you just want to check whether anything is created in JSONObject or not you can use length(), because by default when JSONObject is initialized and no key is inserted, it just has empty braces {} and using has(String key) doesn't make any sense.
So you can directly write if (jsonObject.length() > 0) and do your things.
Happy learning!
You can use the JsonNode#hasNonNull(String fieldName), it mix the has method and the verification if it is a null value or not

Dapper return result fails after enumeration

I have a dapper query multiple function that outputs a number of different lists except for the very first list. While debugging I discovered that when the code gets to the following line in Dapper the results disappear:
public IEnumerable<T> Read<T>....
var result = ReadDeferred<T>(gridIndex, deserializer.Func, typedIdentity); //result has correct db values here
return buffered ? result.ToList() : result; //result = Enumeration yielded no results
The ReadDeferred function does not process any code in the try or finally clause. Why is the value of result being lost in enumeration?
Here is my code that calls dapper:
var results = con.QueryMultiple("GetInspections", p, commandType: CommandType.StoredProcedure, commandTimeout: 5000);
var inspectionDetails = new Inspection
InspectionDetailList = results.Read<Inspection>().ToList(), <-- this one does not popuplate
SOHList = results.Read<SOHPrograms>().ToList(),
BuildingList = results.Read<Building>().ToList(),
AdministratorList = results.Read<Employee>().ToList(),
NotAdminList = results.Read<Employee>().ToList(),
InspectionList = results.Read<InspectionList>().ToList()
return inspectionDetails;
I have verified that there are result sets being returned for each list from the sql query.
This problem had a two part answer, because I had two problem errors. The first was that I was calling the InspectionDetailList as a list from inside the Inspection object which I removed and the second was to change the code that calls dapper to use a using statement and call the pieces individually. Thanks goes to a friend and one of the overflow posts found here.
using(var results = con.QueryMultiple("GetInspections", p, commandType: CommandType.StoredProcedure, commandTimeout: 5000))
var inspectionDetails = results.Read<Inspection>().First();
inspectionDetails.OshList = results.Read<SOHPrograms>.ToList();
inspectionDetails.BuildingList = results.Read<Building>.ToList();

ScriptDb, how to tell a object has a key or not

I have such objects in ScriptDb,
How do I query object without key c?
It seems the only way is to query all object using db.query({}), then use something like "typeof result.c == 'undefined'".
Is there a way to do it in ScriptDb?
You can use that to get records without c:
var db = ScriptDb.getMyDb();
var result = db.query({c: db.not(db.anyValue())});
while (result.hasNext()) {
var current = result.next();
Logger.log ("a= "+current.a+", c="+current.c);
The ones with c:
var result = db.query({c: db.anyValue()});
These functions (not, anyValue...) are documented in Class ScriptDbInstance

RIA-Services - how to WhereOr or use an IN style construct

I am using SL 4, WCF RIA Services against Entity Framework 4.0. I have an Entity, Visit, that has a string Status field. I have a search screen where I need to display results that have StatusA or StatusB. I am struggling to find a way to specify a client-side query that specifies a collection of statuses that should be matched. If I was to write what I want in SQL it would look something like:
select * from Visit where Status in ('StatusA', 'StatusB');
Client side, it appears to be straightforward to chain Where methods for a WhereAnd effect:
var query = this.PqContext.GetVisitsQuery();
if (!string.IsNullOrEmpty(this.PracticeName))
query = query.Where(v => v.PracticeName.ToUpper().Contains(this.PracticeName.ToUpper()));
if (this.VisitDateAfter.HasValue)
query = query.Where(v => v.VisitDate > this.VisitDateAfter);
if (this.VisitDateBefore.HasValue)
query = query.Where(v => v.VisitDate < this.VisitDateBefore);
However, I can't seem to find a straightforward way to do a WhereOr style operation. I have tried this:
var statuses = new List<string>();
if (this.ShowStatusA)
if (this.ShowStatusB)
if (statuses.Any())
query = query.Where(BuildContainsExpression<Visit, string>(e => e.Status, statuses));
Where BuildContainsExpression looks like:
private static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
if (null == valueSelector)
throw new ArgumentNullException("valueSelector");
if (null == values)
throw new ArgumentNullException("values");
ParameterExpression p = valueSelector.Parameters.Single();
if (!values.Any())
return e => false;
var equals =
value =>
(Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate<Expression>(Expression.Or);
return Expression.Lambda<Func<TElement, bool>>(body, p);
But this throws a "Bitwise operators are not supported in queries." exception. Any clues? Is there an alternative way to build an expression tree that works here or do I need to pass all the parameters over to the server and use the BuildContainsExpression there?
Your time and your guidance are much appreciated.
You can create a query method such as the following in your domain service:
GetVisitsByStatus(string[] statusList) {
// create the LINQ where clause here
And then from the client, call context.GetVistsByStatusQuery(string[]).
Not all of LINQ is (or even can) be exposed over the URL, so there are always cases where you need to use simple parameters, and have the middle tier construct the LINQ expressions that eventually define the query that goes to the back-end data store.
Hope that helps.