How is a #Model count subtracted - asp.net-mvc

How is a #Model count subtracted from another #Model count to display the difference. For example the two counts below. So answer should be 0
`#Model.Where(x=> x.Product != null).Count(x=> x.Product.name)
Subtract
#Model.Count(x=> x.Product.name)`

You need to use the - operator, just like any other C# code.
However, you also need to wrap the entire expression in parentheses to prevent Razor from treating the - as markup:
#(a - b)

Related

Laravel 5.1 how to make my eloquent query closure secured

what i'm trying to do is to Sum the total deposits of each reservation model, with the condition of less than the amount input in the text.
here's my query:
$reservations->whereHas('deposits', function($query) use ($etc_filters){
$query->havingRaw('SUM(amount) <= '.$etc_filters);
});
as you can see, i'm using havingRaw that can be injected with another query. right now i cant find any alternative solution for my code.
You can use the second argument the havingRaw method accepts, to make the value a binding, which gets escaped before it is inserted in the query:
$reservations->whereHas('deposits', function($query) use ($etc_filters){
$query->havingRaw('SUM(amount) <= ?', $etc_filters);
});

Dart: DoubleLinkedQueue length after entry prepend

I'm manipulating entries inside a DoubleLinkedQueue via the DoubleLinkedQueueElement.append/prepend methods. This results in the new elements being inserted into the queue, but fails to update the length, and the toList() method results in an error being thrown.
I understand queues are only supposed to have elements added at the start/end, but it looks like the interface should allow for adding in the middle via the entries. I find it hard to believe such a common/well understood data structure would have a bug at this point - so am I using DoubleLinkedQueues incorrectly? Is there another data structure that I should be using? I'm looking to merge values from another iterable into my own sorted iterable - a SplayTreeSet might get me there in n log n time, but a simple merge should get me there in linear time...
Example of code that acts unexpectedly:
main() {
var q = new DoubleLinkedQueue<int>.from([1]);
q.firstEntry().prepend(0);
print('length: ${q.length}');
int i = 0;
for (var qi in q){
print('${i++}: $qi');
}
}
Output:
length: 1
0: 0
1: 1
It looks like the length getter is only pointing to an internal counter. This is done because counting the elements everytime might take very long for long lists.
The internal counter is only updated if you use the methods that directly operate on the list instead of using the prepend method of an element. In your example you should use q.addFirst(0); which results in the length being updates. The .prepend() method just inserts a new element and changes the pointer. Which results in correct traversation of the elements, but the counter is wrong anyway.
Unfortunately it looks like you cannot insert elements in the middle of the list, nor can you make the list recount the elements. You should consider creating a bug over at www.dartbug.com.
// Update:
toList() throws an error because there are more elements than length.

How to remove the trailing comma & space in a for-each loop that generates links in a mvc vbhtml view?

Given a loop like:
#For Each x In item.PostCategory
Dim cats = x.CategoryName & ", "
#Html.ActionLink(cats,
"PostsByCategory", "Posts", New With {.Category = x.CategoryName.ToSeoUrl,
.Page = Nothing}, Nothing)
Next
I need to remove only the last comma and space - everything I have tried removes the commas and spaces in the middle as well as the end. The loop renders categories and I want them separated by a comma and space but do not need or want the trailing comma and space. Each category needs to be a separate link so string.join won't work. I tried trim.substring - that removes the commas in the middle. TrimEnd did not work. I have searched and have not found a solution.
Instead of World, Science, - i want World, Science
You can try to check if x is the last item in PostCategory. If it is true, then append an empty string, else append comma and space :
Dim cats = x.CategoryName & IIf(x.Equals(item.PostCategory.Last()), "", ", ")
There's many different ways to solve this problem. You'll have to determine the best way. Simply, you can just not use a foreach and do a simple for instead. Then you can easily tell if you're on the last item by comparing the index with the count and conditional show or not show the comma based on that.
Alternatively, you can construct a list of the string values this code would otherwise render directly to the page and then use string.Join to join the list items separated by ", ". string.Join never appends the delimiter to the end, so that fixes your problem.
You could also go fancier with some sort of editor template or partial view or even create a HtmlHelper extension. If just depends on how you want to handle it.

comparing Decimal(9,3) column value in Linq

I have a column pointsAwarded decimal(9,3) and I have the following LInq
db.TableName.Select(x=>x.pointsAwarded >0)
The fact is that it is not filtering the data and returning me the whole result set.
How to compare it?
I tried with x.pointsAwarded.value>0 and x.pointsAwarded.value>0.000 and
x.pointsAwarded > (Decimal?)0
but with no luck.
Pls help
Try using a Where instead of a Select
db.TableName.Where(x=>x.pointsAwarded > 0)
UPDATE:
This answer has been given more credit than it previously deserved, so I will elaburete a little
The Where statement acts as the filter. It determins whitch elements the returned list should contain.
The Select statement is the projection of the elements. Given a list of elements, how would you like them presented.

Using dustjs how do I show a block of code when an array's size is greater than one

I am using dust 1.1.1. I've tried nesting the #size help inside of an #if but that broke.
Another trick to it is once I've determined the length is greater than one I then want to iterate through the array.
The Dust grammar does not allow a helper to be used inside another helper, except in the body.
Instead, I would try something like this:
{#myArray}
{#gt key=$len value=1}{.}{/gt}
{/myArray}
This will only output the value of the element in the array if the length of the array ($len) is greater than 1.
My workaround for this was:
{?myArray[1]}
{!Do greater than one stuff!}
{/myArray[1]}

Resources