Browsing 7239 questions and answers with Jon Skeet

LINQ GroupBy and then OrderBy a property from the previous query

So I have a CategoryAttribute class: [AttributeUsage(AttributeTargets.Field | AttributeTargets.Method, AllowMultiple = false)] public class CategoryAttribute :...
Jon Skeet
people
quotationmark

If this is in LINQ to Objects, you could just use: .OrderBy(g => x.First().cat.order) Alternatively, you could group by both the name and the order, then use the order part of the key: .GroupBy(x => new { x.cat.name, x.cat.order... more 6/5/2014 10:07:36 AM

people

I want to be able to derive from a class internally, but disallow class in other assemblies to derive from the class

Hej I have the following setup: Assembly 1 public abstract class XX<T> : XX where T: YY { } public abstract class XX {} Assembly 2 public class ZZ : YY {} public class...
Jon Skeet
people
quotationmark

You can create an internal constructor in XX: public abstract class XX { internal XX() { } } (Or if you already have explicitly-declared constructors, make them all private or internal.) That will prevent any other... more 6/5/2014 9:26:05 AM

people

Is there a way to order by an IQueryable with a different amount of ordering

For example, I want to try something like this. The sorting par might have none, 1 or several sorting columns by different order. But I can't use the ThenBy method as it's only...
Jon Skeet
people
quotationmark

AgentFire's answer is effectively appropriate, but a bit long-winded to special-case in each situation. I'd add an extension method: EDIT: Apparently, the code below doesn't correctly determine whether the query is already ordered; even... more 6/5/2014 8:43:55 AM

people

why assign ArrayList to new ArrayList temp

I am looking at the code for Permutations problem on leetcode. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. And...
Jon Skeet
people
quotationmark

I have no idea why here needs to assign the "l" to "temp" He's not - that would just be: ArrayList<Integer> temp = l; Instead, the code creates a copy of the content of the list l refers to, in a new ArrayList. That means... more 6/5/2014 6:04:39 AM

people

Generic Method Unable to cast object of type 'System.Object' to type X

I am new to creating generic methods and have come across casting issues. The following is a class library for communicating with an API. The object I am trying to cast to lives...
Jon Skeet
people
quotationmark

The object I am trying to cast to lives in my main application It's not entirely clear what you mean by that, but the object you're trying to cast is just an instance of System.Object: (T)new object() That can never work unless T... more 6/5/2014 6:01:20 AM

people

How to get JUnit 4.11 to recognize class in default package?

Similar to other questions on this subject, I am having problems getting JUnit 4.11 to recognize my class. Similar to the question found here, I am using algs4.jar to write a...
Jon Skeet
people
quotationmark

Look at your command-line: java -cp /Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore You've got the JUnit jar file on your class path - but not the current directory. So it won't be able to find any classes... more 6/4/2014 6:42:26 PM

people

C# .Net 4.0 mscorlib contains ReadOnlyDictionary?

I need a ReadOnlyDictionary. All posts told me that there is none in .Net 4.0. For that reason I created my own implementation. After doing that Resharper 5.1 told me about an...
Jon Skeet
people
quotationmark

ReadOnlyDictionary was introduced in .NET 4.5. Now .NET 4.5 is an "over the top" install, which is why you're seeing it in your version of mscorlib. However, if your project is targeting .NET 4.0, then presumably Visual Studio has some... more 6/4/2014 3:16:58 PM

people

Jaxb marshalling to wrong character

I used to marshall my models by calling there toXml(): @XmlRootElement public class MyModel { private String body; public String getBody() { return this.body; ...
Jon Skeet
people
quotationmark

Okay, now we know how you're writing the file, this is the problem: FileWriter w = new FileWriter("D:\\outtest.xml"); That will always use the platform-default encoding - even though you've told JAXB that you're going to be using UTF-8.... more 6/4/2014 1:51:09 PM

people

Method local variable being modified inside a Parallel.For. How thread safe is this?

I have this code snippet: int totalData = result.Data.Count; int count = 0; Parallel.ForEach(result.Data, data => { try { EventRange importedEntity =...
Jon Skeet
people
quotationmark

It's not at all thread-safe at the moment. You can use Interlocked.Increment(ref count) instead, but it's generally better to have a "local" value for each thread, and sum them at the end. That way you don't need to have any sort of... more 6/4/2014 1:44:02 PM

people

How do I access an object method inside a List?

I am creating a website using jsp. I have a method that returns each row from a JDBC query as an object. Each object is added to a list and returned to the jsp file. This is the...
Jon Skeet
people
quotationmark

The problem is that your getCategories method returns a List<Object>, when it would make more sense for it to return List<Category>: public List<Object> getCategories() throws Exception{ // Note: this is now a... more 6/4/2014 11:22:09 AM

people