Browsing 7239 questions and answers with Jon Skeet

Wait for first task in array to match a condition

I have a Task<bool>[] myTasks. How do I get notified (await) when the first Task returns true ?
Jon Skeet
people
quotationmark

Basically, you need to keep a set of incomplete tasks, and repeatedly use Task.WhenAny, check the result, and keep going (having removed that task) if the result wasn't what you were looking for. For example: ISet<Task<bool>>... more 11/16/2016 7:14:21 AM

people

How to use inline method when assigning to object

With LINQ to objects is there a way to assign a property using an inline method without defining the method separately? For example, I have a list I am iterating over (results)...
Jon Skeet
people
quotationmark

If you're using LINQ to Objects, your lambda expression only needs to be convertible to a delegate. So you can use something like: var testObject = results.Select(row => { // Several statements here, as you wish int x = row.X; ... more 11/14/2016 9:29:42 PM

people

Java: How/When does super() of child class constructor participate in instance flow?

Code: Parent Class: public class Parent { int i = 10; { System.out.println(i); } public Parent(){ ...
Jon Skeet
people
quotationmark

The super() line has already taken effect in step 4, basically. Just super(); is implicit and would usually be removed. Typically you use explicit constructor chaining for: Chaining to a constructor in the same class Providing arguments... more 11/14/2016 9:41:01 AM

people

LINQ Case statement with COUNT and GROUP

Can you help me translate this Mysql into Linq please : SELECT distcode, COUNT(cid) as count_all_case ,count(case when labid = 1 then cid end) as count_id1 ,count(case when labid...
Jon Skeet
people
quotationmark

Well, you'd need to look at the generated SQL, but the filtering and grouping parts are simple - it's only the counting bit that's particularly tricky. You can probably do that within the projection of the group: var start = new... more 11/14/2016 7:47:25 AM

people

Convert List<Property> To JObject

Consider this code: var joWork = ((JObject) x).Properties() .Where(p => p.Value.Type == JTokenType.String).ToList(); I end up with a List<JProperty>. Is there a...
Jon Skeet
people
quotationmark

Yup, you can just pass the list into the JObject constructor, and Json.NET will do the rest. Here's an example: using System; using System.Linq; using Newtonsoft.Json.Linq; public class Test { static void Main() { JObject... more 11/9/2016 7:41:32 PM

people

Conditional update strings in collection with LINQ

I want to update all strings in a List, that do not start with "http://" to start with "http://" In a foreach I would do something like this: url = url.StartsWith("http://") ?...
Jon Skeet
people
quotationmark

Just use a regular for loop - that's the simplest way of modifying the collection: for (int i = 0; i < list.Count; i++) { string url = list[i]; if (!url.StartsWith("http://")) { list[i] = "http://" + url; ... more 11/8/2016 3:04:48 PM

people

Delegates are immutable but how?

What happens when I add a method to existing delegate? I mean when I added the method1 to del, del holds the address of method1. When I add method2 afterwards, del still points to...
Jon Skeet
people
quotationmark

You're not changing the Delegate object - you're changing del to refer to a different object. It's exactly the same as with strings. Let's convert your code into the same thing but with strings: string str = "x"; str += "y"; str +=... more 11/8/2016 9:09:52 AM

people

Are expression trees thread safe?

I want to cache some expressions that are generated dynamically (with LinqKit) in order to pass them to a Where clause that is part of an Entity Framework query. So I have...
Jon Skeet
people
quotationmark

Expression trees themselves are immutable. However, they can refer to things that do change, e.g. using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; public class Test { static void... more 11/5/2016 7:31:38 PM

people

wait() , notify() which thread first unlock?

Trying to understand wait() and notify(). I know when thread A went to wait() it will be waked up by notify() from other thread. But what will happens if threads A,B,C went to...
Jon Skeet
people
quotationmark

From the documentation for notify(), emphasis mine: Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs... more 11/5/2016 10:15:16 AM

people

Would AddRange() be faster than ToList() in this case?

I have a comma delimited string called ctext which I want to split and put into a List<string>. Would using LINQ, List<string> f = ctext.Split(',').ToList(); be...
Jon Skeet
people
quotationmark

Fortunately, we can easily look at what ToList does now that it's open source. (Follow the link for the latest source...) I haven't seen IListProvider<T> before, but I doubt that an array implements it, which means we've basically... more 11/4/2016 7:06:10 PM

people