Browsing 7239 questions and answers with Jon Skeet

Why DbSet<t>.Where() used IQueryable version by default?

Whenever we want to use IEnumerable extension methods instead IQueryable version, we have use AsEnumerable() method. var list = context.Details.AsEnumerable().Where(x =>...
Jon Skeet
people
quotationmark

You'd usually want to use the IQueryable form, so that the filtering is done on the database instead of locally. The code you've got will pull all records down to the client, and filter them there - that's extremely inefficient. You... more 3/3/2015 11:35:17 AM

people

Why is the following Vector code broken exactly?

follows from : Synchronization in Vectors in Java Why is the below code broken as far as synchronization is considered? Is the vector class not synchronized on its own object...
Jon Skeet
people
quotationmark

Is the vector class not synchronized on its own object (this)? Yes, but only for each individual operation. Here we have two operations: if (vector.isEmpty()) vector.add(anElement); It's possible that between the check of... more 3/3/2015 10:33:44 AM

people

Comparing GenericTypeDefinition of interfaces

Simple code that I expect List<int>'s GenericTypeDefinition to contain a generic interface of ICollection<>. Yet I can't derive an acceptable type from List<int>...
Jon Skeet
people
quotationmark

No, the generic type definition will refer to ICollection<T> specifically, where T is the type parameter for the IList<T>. Imagine you had something like: public class Foo<T1, T2> : IEnumerable<T1>,... more 3/3/2015 9:57:27 AM

people

unable to access nested class outside class

I know that its not a good practice to nest a class inside another class, but following is just for fun. I have the following code namespace PlayIt { class Class1 { ...
Jon Skeet
people
quotationmark

I am able to create the object of class1 but not of class2, Why So? Two reasons: Firstly, Class1 is implicitly internal, whereas Class2 is implicitly private (because it's nested). Secondly, you're trying to use just Class2 in a... more 3/3/2015 8:24:22 AM

people

where should we use commit(session), in try or finally?

If I want to use commit(session) after successful execution of database operation, where its better to put it in try or in finally block ? Here I used it in finally, should it be...
Jon Skeet
people
quotationmark

It should be in try, for two reasons: You'll commit the session if some exception or error other than a HibernateException, and you almost certainly don't want to do that You'll call commit after calling rollback. I can't remember... more 3/3/2015 7:07:28 AM

people

Linq Where Condtion For Different Types

I have a list that is derived from ProfileBase. This list can contains an instance of Profile,,DynamicViewProfile because both are derived from ProfileBase But If the item type...
Jon Skeet
people
quotationmark

There's nothing special about LINQ here - you basically write the same code as you would normally, using is or as: string searchText = searchBar.Text; profileListsForSearch = profileLists .Where(profile => profile is... more 3/3/2015 6:59:45 AM

people

Sorting xml using linq, but getting a null error?

Trying to sort an xml file based on a Time Field (soonest first)... a snippet of the xml is here: (The element I want to order on is TimeEventStart) <Bookings> ...
Jon Skeet
people
quotationmark

You're going in one level too deep - trans is already a Data element, you're then ordering the elements within Data... by a TimeEventStart element within each of those subelements... and that doesn't exist, because TimeEventStart is a... more 3/2/2015 9:49:32 PM

people

Should i prefer readability over safety in my code?

I am dealing with some code that has consistent pattern of creating Map instances like this: Map<String, String> valuesMap = new HashMap<String,...
Jon Skeet
people
quotationmark

Despite the various comments, I've seen helper methods like this used quite a bit, for static initialization, and usually found them simpler than alternatives. Of course, it only works when your key and value type are the same - otherwise... more 3/2/2015 7:27:07 PM

people

How do I get the closest DateTime from a List<DateTime>

Say I have the most recent DateTime and a List of all the possible dates. How would I efficiently go about finding the closest date time to last year's date in the list? Say my...
Jon Skeet
people
quotationmark

As it's sorted, you can use a binary search to try to find an exact match. If List<T>.BinarySearch returns a non-negative number, you know you've found an exact match. Otherwise, you can apply the bitwise complement operator to find... more 3/2/2015 7:13:05 PM

people

await Task.WhenAll(tasks) Exception Handling, log all exceptions from the tasks

I am trying to figure out how to report all exceptions thrown by a list of tasks from the code below. The basic idea of this code snippet is: The user sends a request to the...
Jon Skeet
people
quotationmark

You've fallen foul of lazy evaluation - the result of Select will create a new set of tasks each time you iterate over it. You can fix this just by calling ToList(): var tasks = _factory.CreateMessage(settings) ... more 3/2/2015 4:38:25 PM

people