Browsing 7239 questions and answers with Jon Skeet

C# explicit cast from collection of KeyValuerPair to Dictionary

I have a list of KeyValuePairs. I normally would use ToDictionary. However I just noted that the error message (shown below) has something about explicit cast, which implies I...
Jon Skeet
people
quotationmark

Implies I can actually cast list to dictionary Well, it implies that the cast would be valid at compile-time. It doesn't mean it will work at execution time. It's possible that this code could... more 6/19/2014 3:59:20 PM

people

How to remove xmlns attribute of a node other than root in an XDocument

Situation I'm using XDocument to try and remove an xmlns="" attribute on the first inner node: <Root xmlns="http://my.namespace"> <Firstelement xmlns=""> ...
Jon Skeet
people
quotationmark

I think the code below is what you want. You need to put each element into the right namespace, and remove any xmlns='' attributes for the affected elements. The latter part is required as otherwise LINQ to XML basically tries to leave you... more 6/19/2014 2:37:10 PM

people

Java NullPointerException in reading

I have problem with reading from my socket i read only if value isn't null but it doesnt't works. @Override public void run() { System.out.println("Reading from socket"); ...
Jon Skeet
people
quotationmark

If br.readLine() returns null, then calling .equals(null) on it will throw an exception - it won't return true. You just want to compare reference identity with null. Calling .equals(null) is never useful, unless you're testing that your... more 6/19/2014 2:17:39 PM

people

POST Request failing due to GET request not allowed

I have a WebMethod in my code behind I'm calling via AJAX. The method works when using a GET request but I'd prefer to use POST and I'd also like to know why this doesn't work...
Jon Skeet
people
quotationmark

Looking at some jQuery documentation I think you're using the wrong property. I suspect this: method: "POST" should be type: "POST" method feels like the more sensible name to me too, but there we go... (Disclaimer: I've never used... more 6/19/2014 1:43:36 PM

people

How to make a non thread safe 3rd party library actually thread safe?

I have a 3rd party library that offers a class. Their documentation says: Thread Safety Public static (Shared in Visual Basic) members of this type are safe for...
Jon Skeet
people
quotationmark

Assuming you don't need the objects to interact with each other at all, ThreadLocal<T> is a very good choice here. It obviously doesn't work if modifications made in one thread are meant to be seen in another thread, just... more 6/19/2014 12:49:21 PM

people

Parallel.Invoke does not wait for async methods to complete

I have an application that pulls a fair amount of data from different sources. A local database, a networked database, and a web query. Any of these can take a few seconds to...
Jon Skeet
people
quotationmark

It sounds like you really want something like: spinner.IsBusy = true; try { Task t1 = Task.Run(() => dataX = loadX()); Task t2 = Task.Run(() => dataY = loadY()); Task t3 = Task.Run(() => dataZ = loadZ()); await... more 6/19/2014 12:32:08 PM

people

DateTime Parse to US format 'error', returns me non US format

I have a problem parsing string to DateTime to US format. Although I provide the string in format of MM/dd/yyyy it keeps returning me the DateTime in format of dd/MM/yyyy and I...
Jon Skeet
people
quotationmark

all return me non us format No, they return you a DateTime value. A DateTime value doesn't have a format. It's just a date and a time. When you want to convert that back into a string, it will use the default format for your current... more 6/19/2014 10:34:46 AM

people

Cannot implicitly convert type IEnumerable to IQueryable with Entities

I am completely new to Entities (I've taken over another persons project) and therefore I am prolly missing some key facts but I'll try and explain my problem: I have applicants...
Jon Skeet
people
quotationmark

This is the problem: select new { ... } That's creating an instance of an anonymous type. How do you expect that to create a Business.Entities.EF.Applicant object? You may well just need to change your code to: select new... more 6/19/2014 10:25:57 AM

people

Wrong Value after type casting in 32 bit process

Please see the following code in C#. float a = 10.0f; float b = 0.1f; float c = a / b; int indirect = (int)(c); // Value of indirect is...
Jon Skeet
people
quotationmark

When you operate directly, it's permittable for operations to be performed at a higher precision, and for that higher precision to be continued for multiple operations. From section 4.1.6 of the C# 5 specification: Floating-point... more 6/19/2014 10:13:12 AM

people

Cast Object into Generic List to get List of Items

Is there any way that I can cast unknown object into generic list to get list of items from object by given ProperyName. Object can be any generic list like List<Country>,...
Jon Skeet
people
quotationmark

You'd have to use reflection, basically. For example: // This assumes it really is just a List<T>... IEnumerable list = (IEnumerable) dataSource; Type elementType = list.GetType().GetGenericArguments()[0]; PropertyInfo property =... more 6/19/2014 9:05:28 AM

people