Browsing 7239 questions and answers with Jon Skeet

Corrupt ZIP file if calling Save twice

I am using DotNetZip 1.9.6 in my application which uses a file structure similar to e.g. *.docx: Zip file containing XML files. Now every module of the application can store such...
Jon Skeet
people
quotationmark

This looks like it's a bug in the library, around removing an entry. If you just remove an entry and then save again, it correctly removes the file. However, if you remove an entry and then add another one with the same name - which is... more 9/23/2015 9:01:31 AM

people

Cast generic parameter

I get an object back from a method. The object is of type object and I can't change this because of backwards compatibility. If it is of a certain type (Response<T> bellow)...
Jon Skeet
people
quotationmark

To get compile-time checking of the name of the property, you can keep the dynamic typing, but get the runtime "mini-compiler" to do the hard work: object data = LegacyCode(); object payload = GetPayload(data); // Use... more 9/22/2015 9:27:02 PM

people

C# Set File Size

I have an app which I use for downloads. I've noticed that if I let it create the placeholder file, then pause it, it still shows the placeholder file size as the size that it...
Jon Skeet
people
quotationmark

You can use FileStream.SetLength: If the given value is less than the current length of the stream, the stream is truncated. In this scenario, if the current position is greater than the new length, the current position is moved to the... more 9/22/2015 8:01:13 PM

people

Dynamic Property Assignment Throws RuntimeBinderException

I am getting a RuntimeBinderException with the message Cannot implicitly convert type 'object' to 'MyNamespace.SomeEnum?'. An explicit conversion exists (are you missing a...
Jon Skeet
people
quotationmark

The compile-time type of the expression on the RHS of the = operator for the first two lines is dynamic. In the first case that's because you've cast to dynamic, and in the second case it's because you're using a dynamic value in one of... more 9/22/2015 5:22:48 PM

people

Linq to Dictionary with key and a list

My data is as under in two tables Master Columns ID MyDateTime 1 07 Sept 2 08 Sept MyDatetime column in above has unique index Detail Columns ID Data1 ...
Jon Skeet
people
quotationmark

This is precisely what lookups are for - so use ToLookup instead of ToDictionary: ILookup<DateTime, Detail> result = (from master in db.master join details in db.detail on master.ID equals detail.ID select new {... more 9/22/2015 12:39:10 PM

people

Date changes while exporting from sql server to csv using talend

I am exporting data from SQL Server in csv fle using Talend. The date format I specified in tMap component is "YYYY-MM-DD 00:00:00.000". The date in SQL is 2015-04-28 00:00:00.000...
Jon Skeet
people
quotationmark

Assuming Talend is just using the format patterns of SimpleDateFormat, you want yyyy-MM-dd 00:00:00.000 - the problem you're seeing at the moment is that D means day of year, not day of month. That's why you're getting 118 - because April... more 9/22/2015 12:24:20 PM

people

Is their an existing method(s) in .Net C# that add up Gregorian duration to start date?

I was looking for existing method(s) in .Net C# that add up Gregorian duration ISO 8681 to start date to come up with end date. For Example : Start Date : 2007-03-01 Duration ...
Jon Skeet
people
quotationmark

My Noda Time project is able to parse ISO-8601 durations as Period objects, which can then be used for calendrical arithmetic with LocalDate or LocalDateTime values. For example: var pattern = PeriodPattern.NormalizingIsoPattern; var text... more 9/22/2015 10:34:22 AM

people

How to check DST status for specified timezones

I would like to check if any specified timezone (other than my local time zone) is in DST or not. To check if the local time zone is currently DST I am using: Dim today_date As...
Jon Skeet
people
quotationmark

You shouldn't do it for a date, as that can be ambiguous (the start of the day in standard time and the end of the day in daylight time, or vice versa) but instead ask whether a specific point in time is in daylight time: Dim zoneId As... more 9/22/2015 9:22:21 AM

people

Using Linq to XML, how can I select the elements that have exactly x number of parent elements?

I am trying to use Linq to XML in my c# program to extract element names from an auto-generated XSD file. This means I cannot do queries based on element names/ contents, and...
Jon Skeet
people
quotationmark

The simplest way to measure "depth" is to count ancestors: var elementsAtDepth8 = xsd.Descendants() .Where(x => x.Ancestors().Count() == 8); No need to write your own recursive code at all. Another slightly... more 9/22/2015 8:57:25 AM

people

Using NodaTime how do I compare the local time now to a fixed time?

I want to be able to compare the current local time in C# using NodaTime to a fixed local time during the day. I do not need to worry about time zones or daylight savings, I just...
Jon Skeet
people
quotationmark

To get the local system time, you do need to worry about the time zone. You can use: var clock = SystemClock.Instance; // Or inject it, preferrably // Note that this *could* throw an exception. You could use //... more 9/22/2015 7:23:53 AM

people