How to increment a counter in LINQ

1,430 views
Skip to first unread message

Rasmus Svendsen

unread,
Apr 24, 2014, 3:11:25 AM4/24/14
to csharp-dr...@lists.datastax.com
How to increment a counter in LINQ? 
I cannot find a way to increment a counter the atomic way - like in CLI: “INCR page_view_counts['www.datastax.com'][home] BY 1”.

If the C# driver not support it yet, I hope for an implementation like this:

  public static CqlQuery<TSource> Inc<TSource, TResult>(this CqlQuery<TSource> source, Expression<Func<TSource, TResult>> memberExpression, TResult value)
  {
   // some logic
  }

The linq syntax will be simple like this:

  StatisticsTable.Where(x => x.author_id == "123").Inc(x => x.followers_count, -1).Inc(x=>x.tweets_count, 15).Update().Execute();



I use the beta version 2.0.0-beta1. Is there a way to increment a counter already?

Cheers,
Rasmus

Jorge Bay Gondra

unread,
Apr 24, 2014, 3:32:28 AM4/24/14
to csharp-dr...@lists.datastax.com
Hi Rasmus,
Counter columns are implemented using an Attribute: CounterAttribute.
If you mark the column of your entity as [Counter], after you increase it / decrease it like a regular column, it will get the diff and update the value accordingly.

Cheers,
Jorge


To unsubscribe from this group and stop receiving emails from it, send an email to csharp-driver-u...@lists.datastax.com.

Rasmus Svendsen

unread,
Apr 24, 2014, 4:28:08 AM4/24/14
to csharp-dr...@lists.datastax.com
Hi Jorge,
Thanks for the fast reply. I have the [Counter] attribute in my data model, but I still not get it to work.

As I understand your answer I shall create a Linq Context, but by doing so I have to get an object down on the client side made ​​the change of the counter and then call the SaveChanges(..) witch update the db, right?
I want to update the counters without having to first get the value down on the client side, like the atomic call. How to do that in the C# driver?

Cheers,
Rasmus

Jorge Bay Gondra

unread,
Apr 24, 2014, 5:23:19 AM4/24/14
to csharp-dr...@lists.datastax.com
Hi Rasmus,
Without a context, as a Linq CqlQuery, it is not possible.
I agree it would be nice to have.

Best,
Jorge

Rasmus Svendsen

unread,
Apr 24, 2014, 5:46:50 AM4/24/14
to csharp-dr...@lists.datastax.com
Hi Jorge,
Okay, I hope for a implementation of increase/decrease of counters in Linq CqlQuery in the near future ;-)

I have found the "Playground" project in github.
When I run the code it complete without errors, but when looking at the output at "After deletion of all tweets our \"Statistics\" table looks like" have the same tweets_counts as before the deletion, hmm...
Maybe there are a bug in the C# driver?

Cheers,
Rasmus

Jorge Bay Gondra

unread,
Apr 24, 2014, 8:06:19 AM4/24/14
to csharp-dr...@lists.datastax.com
Hi Rasmus,
Yes, it was indeed a bug: when maintaining the same entities in context after executing the first batches, when unmodified in following batches, it was removed from context.
I'll push a fix right away.

Thanks for your help,
Jorge


To unsubscribe from this group and stop receiving emails from it, send an email to csharp-driver-u...@lists.datastax.com.

Yann ROBIN

unread,
Aug 28, 2014, 10:12:16 AM8/28/14
to csharp-dr...@lists.datastax.com
Hi,

I've submitted a patch to support counter in Linq :

It's very simple, syntax would be :
query = table
    .Where(r => r.RowKey1 == 5 && r.RowKey2 == 6)
    .Select(r => new CounterTestTable1() { Value = 1 })
.Update();

And it translate to :
UPDATE "CounterTestTable1" SET "Value" = "Value" + 1 WHERE "RowKey1" = 5 AND "RowKey2" = 6

-- 
Yann
Reply all
Reply to author
Forward
0 new messages