Browsing 7239 questions and answers with Jon Skeet

Does Any() run the LINQ query

If I write a query for example: var query = (from a in this.Context.Addresses where a.Active select a.Name); then is it correct to say if(query.Any()) return...
Jon Skeet
people
quotationmark

Yes - it has to, given that it's got to return true or false. It can't possibly do that without running the query. If you're going to conditionally return ToList(), you'd be better off writing: var list = query.ToList(); return... more 5/5/2015 1:50:30 PM

people

Date to XMLGregorianCalendar conversion giving wrong results

I have a code snippet that will convert Date to XMLGregorianCalendar, but after seeing the results dates are different. Not sure why change in dates after...
Jon Skeet
people
quotationmark

The problem is that you're not specifying a time zone in your DateFormat - so the system default time zone is being used. The result is midnight in your local time zone (IST) but in the XMLGregorianCalendar, it's being rendered in... more 5/5/2015 1:24:29 PM

people

Java Class that extends String (or similar)

This is mainly out of curiosity. How would you (if you can) make a class that mimics String. You can't extend String as it's final so that's a no go, but purely for the interest...
Jon Skeet
people
quotationmark

No, it's not possible to introduce your own literal types in Java. The language specification gives the syntax for numeric, character and string literals, and that's it. You can't even add your own implicit conversions from an existing... more 5/5/2015 12:03:48 PM

people

how to prevent unsetting ReadOnly flag of a file using java?

I 've created a servlet to let users download a file . When the file is downloaded , I want it to be ReadOnly so that the user can't modify its content . So I've used the...
Jon Skeet
people
quotationmark

I've created a servlet to let users download a file. That servlet will be running on the web server. It's not running on the user's local computer - so it can't change anything about the user's local file system. Even your... more 5/5/2015 11:01:24 AM

people

How to get timezone daylight saving rules from Noda Time in POSIX time zone format

As answered in Getting Daylight Savings Time Start and End in NodaTime daylight saving start and end dates can be solved for given year. How to get the actual rules for...
Jon Skeet
people
quotationmark

The POSIX format only seems to cater for a single rule - whereas in reality, the rules change over time. It's also not clear to me whether POSIX rules allow for 24:00 as a transition time, and the expectation that all transitions are... more 5/5/2015 10:07:45 AM

people

the type arguments for method system linq enumerable as enumerable Error

DataTable tblFiltered = dtable.AsEnumerable() .Where(row => row.Field<String>("empsalary") > 12000) .OrderByDescending(row =>...
Jon Skeet
people
quotationmark

You haven't told us the type of dtable, but assuming it's DataTable, I suspect you intended to use DataTableExtensions.AsEnumerable rather than Enumerable.AsEnumerable... in which case you're probably just missing either a using directive... more 5/5/2015 9:49:54 AM

people

Force non abstract method to be overridden

I have a method String foo() in an abstract class which already does a few precomputations but can't deliver the final result the method is supposed to return. So what I want is...
Jon Skeet
people
quotationmark

Yes, by redesigning to use the template method pattern and including an abstract method: public abstract class AbstractSuper { public final String foo() { // Maybe do something before calling bar... String... more 5/5/2015 7:33:26 AM

people

java error exception in thread "main" java.util.illegalFormatConversionException

my code compiles but when i try to run it, an error message says there is an exception at main java.util.illegalFormatConversionException. how can i fix this? i am sure i got the...
Jon Skeet
people
quotationmark

Look at this call - I've put the arguments on different lines: String.format( "Total $%.2f", "Total Tax $%.2f", "Grand Total $%.2f", totalCharge, totalTax, grandTotal) That's passing "Total $%.2f" as the format... more 5/5/2015 7:27:12 AM

people

Java timer task: Adjusting daylight saving time

I have a piece of code that has to be executed at a particular time every day. If I schedule it to be executed at 9PM everyday, then it has to work even during the switching of...
Jon Skeet
people
quotationmark

If you need to schedule based on calendrical values - rather than just elapsed time, basically - then you either need to wrap Timer in your own code, or use a library which has already been built for this purpose. In this case, I suspect... more 5/5/2015 6:14:27 AM

people

KeyPress method does not work due to no reference

There are several KeyPress method in my code. All are working except the one due to no reference. I have checked the project for naming mistake. But there is no any mistake. How...
Jon Skeet
people
quotationmark

Looks like you just don't have the event hooked up. Go into the designer, select the btnback button, then in the Events tab of the Properties window, find the KeyPress event and subscribe the btnback_KeyPress method to it as a handler. more 5/2/2015 8:10:27 PM

people