Browsing 7239 questions and answers with Jon Skeet

Proper way to Categorize by a Property

I have made a C# LINQ query that categorizes (or groups) by a property and I am almost sure there is a better way. My project is full of these queries so I am really interested on...
Jon Skeet
people
quotationmark

It sounds like you just want: var result = g.GroupBy(x => x.City, (key, group) => new { city = key, employees = group.Select(emp => new { ... more 1/16/2014 9:46:33 AM

people

How to resolve this ambiguity in generic extension methods?

private static void TestAmbiguousGenerics() { var string1 = "Hello".Foo(s => s + " world"); // works int? n = 123; var string2 = n.Foo(x => (x + 1).ToString());...
Jon Skeet
people
quotationmark

No - both methods are applicable in terms of overload resolution; the type parameter constraints are only checked after the best overload is picked. There's a really evil way of working round this but personally I'd either give the two... more 1/16/2014 9:07:02 AM

people

can enum type derive from interface

I have three enums inside my class public enum CPUBrandEnum { Intel = 1, AMD = 2 } and public enum CPUTypeIntel { Celeron, Pentium4 } public...
Jon Skeet
people
quotationmark

No, enums can't implement interfaces in .NET. They're really just named numbers. Even if they could implement interfaces, you'd end up with the potential for bad data (e.g. an "Intel ASeries". It's not clear what you want to do with... more 1/16/2014 9:04:51 AM

people

CONVERT date format from mm dd yyyy to dd mmm yyyy in c#

I am trying insert asp.net form field values to oracle database table. I have a date field which is in "MM-DD-YYYY" format. I need to add that date to oracle table. So i am trying...
Jon Skeet
people
quotationmark

You need to parse the date using MM-dd-yyyy but then you shouldn't need to format it at all. Just pass it to the database as a DateTime using parameterized SQL. DateTime creationDate; if (DateTime.TryParseExact(CreationDateTextBox.Text,... more 1/16/2014 7:03:17 AM

people

TCP Server application becomes unresponsive

I was trying to make a TCP GUI Server application for windows. But when i am trying to start the server it becomes unresponsive. I couldn't find out what is the problem...
Jon Skeet
people
quotationmark

I couldn't find out what is the problem here Well, you've got this code executing in your UI thread: while (true) { try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch... more 1/16/2014 6:51:18 AM

people

Insert datetime in SQL without millisecond using a parameter

I need to insert a .net DateTime in my SQL Server DataBase without the milliseconds. dtNow = Date.Now myCmd.Parameters.AddWithValue("MyDate", dtNow) I want the values on my...
Jon Skeet
people
quotationmark

While you could subtract the number of milliseconds as suggested in comments, that would still leave you with submillisecond values. That may not cause a problem, but it's possible that the driver will round the submillisecond value up to... more 1/15/2014 4:29:32 PM

people

Displaying elements of an arraylist

I am trying to display the elements of my ArrayList<LibraryItem> in an action listener method by calling a printlibrary method from another class. I keep getting: The...
Jon Skeet
people
quotationmark

Look at your call: library.printLibrary(); Now look at the declaration: public static void printLibrary(ArrayList<LibraryItem>items) This is a static method, with a parameter which is a list. You're trying to call it as an... more 1/15/2014 4:10:20 PM

people

Unparseable date: "1/29/2014 11:45:00 AM" Android

I have a problem with parsing the following date from string: "1/29/2014 11:45:00 AM" I do it the following way: String source = "1/29/2014 11:45:00 AM"; Date startDate; String...
Jon Skeet
people
quotationmark

Your format string specifies that you'll provide a two-digit month, but you're only providing "1". I suspect you want: String sdfPattern = "M/d/yyyy hh:mm:ss aa"; Additionally, the "AM/PM" designator is locale-sensitive (as are the... more 1/15/2014 2:47:00 PM

people

How to implement generic method in C# having multiple constraints

I want to write a generic method: void Foo<T>(IList<T> list) In the method, I want to compare elements of list either by using operator < if T is primitive type...
Jon Skeet
people
quotationmark

Is this possible? If not, it means C# generics is very limited in use I think. Even if it weren't possible, generics can be used in many many situations. However, it's really simple given that all the primitive types implement... more 1/15/2014 1:58:50 PM

people

How to update value in jdbc?

I am getting exception java.lang.illegalargumentexception when I try to update the value of date field. I have a SQL database which have 2 timestamp field format (0000-00-00...
Jon Skeet
people
quotationmark

Look at this code: psmnt= cn.prepareStatement("update Councel set StartDate=?,ExpireDate=? where CouncelRegNo=?"); psmnt.executeUpdate(); You're preparing a statement with three parameters, but not setting any of them. Additionally, I... more 1/15/2014 1:45:58 PM

people