Resource Template - Count array items on condition - azure

Is it possible to count the number of items in an array based upon a certain condition in Resource Templates? Similar to how we can use 'Where-Object' within PowerShell. Seems that the 'length' function is only able to count the total number of items.

No, you cannot do that, unless you hack your way through using nested templates. And that is only possible if you want to compare against a specific object, and you would probably need at least 2 levels of indirection.
And i would generally advice against that, unless there's no other option.
but if you want to do that, you would need this function, nested deployments and ARM template way of doing conditionals and I would argue that you would need a state parameter in the nested templates to share the state between those.

The other answer is pretty much old and is outdated.
The ARM template function length(arg1)returns the number of elements in an array, characters in a string, or root-level properties in an object.
https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-array#length

Related

Cosmos DB: Difference between using MaxItemCount and Take() when retrieving N many records

I need to take the top N records from a Cosmos DB container. I have noticed that I've been using two different patterns in various places in my solution and would like to stick to one approach. So far, I have treated both approaches to be the same, but I'm not 100% confident.
Approach 1:
container.GetItemLinqQueryable<T>(true, continuationToken, requestOptions).Where(...).ToList()
In this approach, I create a QueryRequestOptions object and set MaxItemCount to n to configure how many records I want to retrieve.
Approach 2:
container.GetItemLinqQueryable<T>().Where(...).Take(n).ToList()
This uses the Take() method of LINQ.
Is there any difference between the two approaches in terms of performance or something else I'm not aware of?
Also, another challenge I have is converting these queries to async. I've noticed ToListAsync() isn't available, and I'm thinking I'd need to use the feed iterator which requires several lines of boilerplate code. Is there an easier way to use async here?
Aproach 1 and 2 calls the same code.
All 3 parameters are optional. So there is no need to send any of the parameters if you are just using the defaults.
If you are changing the parameters to something other than the defaults, you need to use approach 1.
It is recommended to always use ToFeedIterator(), and to do the asynchronous execution.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.container.getitemlinqqueryable?view=azure-dotnet

DocumentDB: get all documents of same entity type

I'm storing documents of several different types (entity types?) in a single collection. What would be the best way get all documents of a certain type (like you would do with select * from a table).
Options I see so far:
Include the type as a property. But that would mean a looking into every document when getting the documents, right?
Prepend the type name to the document id and try searching by id with typename*.
Is there a better way to do this?
There's no built-in entity-type property, but you can certainly create your own, and ensure that it's indexed. At this point, it's as straightforward as adding a WHERE clause:
WHERE docs.docType = "SomeType"
Assuming it's a hash-based index, this should provide efficient lookups and filter out unwanted document types.
While you can embed the type into a property (such as document id), you'd then have to do partial string matches, which won't be as efficient as an indexed-property comparison.
If you're curious to know what this query is costing you, the RU value is displayed both in the portal and via x-ms-request-charge return header.
I agree with David's answer and using a single docType field is what I did when I first started using DocumentDB. However, there is another option that I started using after doing some experiments. That is to create an is<Type> field and setting its value to true. This is slightly more efficient for queries than using a single string field, because the indexes themselves are smaller partial indexes, but could potentially take up slightly more storage space.
The other advantage to this approach is that it provides advantages for inheritance and mixins. For example, I have both isLookup=true and isState=true on certain entities. I also have other lookup types. Then in my application code, some behaviors are common for all lookup fields and other behaviors are only applicable to the State type.
If you index the type property on the collection, it will not be a complete scan.

Creating a file that contains the set of inputs in pyqt

I have created a widget with a text bix, a combo box, a checkbox and some pushbottons. I wish to make a record of the set of input given everytime in a file. how do I do that ? Pls suggest.
The easiest and not that bad way imho is by reading the values and serializing them using json. You have to set/get the values individually for each input (with different calls). It's a breeze in fact. Personally, I have made my own 'serializing' function so I keep references to all objects in a list and that function loops the list serializing everything.
Depending on your needs and the complexity of your project you may need something different. Why don't you share some more details?
Best Regards.

Best Practice for Accessing SharePoint List Item through Object Model

I would like to know the best Practice that you guys follow when it comes to access the SharePoint List Items / Doc Lib using Object Model. To start let me share few things I have found.
Limit the number of Items Per container to 2K items.
Use ProcessWebData method of SPWeb to do Update/Insert of Large items
To completely answer your question would require a full blog post. There are several of these out there on the IntraWebs already.
Here are a few of the major points:
Avoid iterating though the entire list unless you need to see every item
If you do iterate through the list, use a foreach loop instead of a for loop
In all other cases use an SPQuery or an SPSiteData query
Access columns by the internal name or the field ID
You should also take a look at Common Coding Issues When Using the SharePoint Object Model as it has some examples on how to avoid serious performance problems.

Optional or boolean elements to specify characteristics in XML schema?

I'm trying to create an XML schema to describe some aspects of hospitals. A hospital may have 24 hour coverage on: emergency services, operating room, pharmacist, etc. The entire list is relatively short - around 10. The coverage may be on more than one of these services.
My question is how best to represent this. I'm thinking along the lines of:
<coverage>
<emergencyServices/>
<operatingRoom/>
</coverage>
Basically, the services are optional and, if they exist, the coverage is offered by the hospital.
Alternatively, I could have:
<coverage>
<emergencyServices>true</emergencyServices>
<operatingRoom>true</operatingRoom>
<pharmacist>false</pharmacist>
</coverage>
In this case, I require all the elements, but a value of false means that the coverage isn't offered.
There are probably other approaches.
What's the best practice for something like this? And, if I use the first option, what type should the elements be in the schema?
Best practice here depends really on the consumer.
The short and simple rule is that markup is for structure, and content is for data. So having them contain xs:boolean values is generally the best course.
Now, on to the options:
Having separate untyped elements is simple and clear; sometimes processing systems may have difficulty reading them, because some XML-relational mappers may not see any data in the elements to put in relational tables. But if they had values, like <emergencyServices>true</emergencyServices>, then the relational table would have a value to hold.
Again, if you have fixed element names, it means if your consumer is using a system that maps the XML to a database, every time you add a service, a schema change will have to be made.
There are several other ways; each has trade-offs:
Using a <xs:string> with an enumeration, and allow multiple copies. Then you could have <coverage>emergencyServices</coverage><coverage>operatingRoom</coverage>. It makes adding to the list simpler, but allows duplicates. This scheme does not require schema changes in the database for the consumer.
You could use attributes on the <coverage> element. They would have a xs:boolean type, but still require a schema change. Of course, this evokes the attribute vs. element argument.
One good resource is Chapter 11 of Effective XML. At least this should be read before making a final decision.

Resources