How do I collect return values from Parallel.ForEach?

You’ve discarded it in here. ParallelLoopResult result = Parallel.ForEach(words, word => AddB(word)); You probably want something like, ParallelLoopResult result = Parallel.ForEach(words, word => { string result = AddB(word); // do something with result }); If you want some sort of collection at the end of this, consider using one of the collections under System.Collections.Concurrent, like … Read more

Better way to query a page of data and get total count in entity framework 4.1?

The following query will get the count and page results in one trip to the database, but if you check the SQL in LINQPad, you’ll see that it’s not very pretty. I can only imagine what it would look like for a more complex query. var query = ctx.People.Where (p => p.Name.StartsWith(“A”)); var page = … Read more

Fastest way to Remove Duplicate Value from a list by lambda

The easiest way to get a new list would be: List<long> unique = longs.Distinct().ToList(); Is that good enough for you, or do you need to mutate the existing list? The latter is significantly more long-winded. Note that Distinct() isn’t guaranteed to preserve the original order, but in the current implementation it will – and that’s … Read more

ASP.NET MVC how to disable automatic caching option?

You can use the OutputCacheAttribute to control server and/or browser caching for specific actions or all actions in a controller. Disable for all actions in a controller [OutputCacheAttribute(VaryByParam = “*”, Duration = 0, NoStore = true)] // will be applied to all actions in MyController, unless those actions override with their own decoration public class … Read more

WPF or Windows Forms

If you’re just starting out, jump straight into WPF. It will allow you to get starting making rich interfaces using mark-up to define your interface without teaching you the possible bad habits (for WPF) that you’d learn from Windows Forms.

C#-How to use empty List as optional parameter

Just use the null coalescing operator and an instance of empty List<string> public void Process(string param1, List<string> param2 = null) { param2 = param2 ?? new List<string>(); // or starting with C# 8 param2 ??= new List<string>(); } The problem with this is that if “param2” is null and you assign a new reference then … Read more

Is it possible to call a C function from C#.Net

The example will be, for Linux: 1) Create a C file, libtest.c with this content: #include <stdio.h> void print(const char *message) { printf(“%s\\n”, message); } That’s a simple pseudo-wrapper for printf. But represents any C function in the library you want to call. If you have a C++ function don’t forget to put extern C … Read more

Cancelling a Task is throwing an exception

I am trying to avoid any exceptions when cancelling. You shouldn’t do that. Throwing OperationCanceledException is the idiomatic way that “the method you called was cancelled” is expressed in TPL. Don’t fight against that – just expect it. It’s a good thing, because it means that when you’ve got multiple operations using the same cancellation … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)