Browsing 7239 questions and answers with Jon Skeet

"Cannot convert lambda expression to type 'string' because it is not a delegate type" querying dataset in C#

I have this code, which compiles fine in VB.NET: Imports System Imports System.Data Imports System.Data.Entity Imports System.Data.SqlClient Imports System.Linq Imports...
Jon Skeet
people
quotationmark

It looks like VB has built-in support for DataTable. Here's a short but complete example: Option Strict On Imports System Imports System.Data Imports System.Linq Public Class LinqTest Shared Sub Main() Dim table as... more 11/18/2015 3:53:53 PM

people

Escaping commas, colons and single quotes with mySQL in C#

--- THIS IS FOR PERSONAL USE, SO DON'T WORRY ABOUT SQL INJECTION --- I've been browsing through several tutorials on mySQL escaping for C# but cannot find one that works for me...
Jon Skeet
people
quotationmark

Don't try to escape anything - just use parameterized SQL. You may not care about SQL injection attacks now, but you will at some point... and by using parameterized SQL for your values, you'll remove both the escaping and injection attack... more 11/18/2015 3:18:36 PM

people

How can I deal with this double.NaN

I have the a Convert.ToDecimal() which occasionally throws an exception with the message Value was either too large or too small for a Decimal , because the call to...
Jon Skeet
people
quotationmark

Okay, so it sounds like you just need to detect whether or not it's a NaN value: double value = DataContainer.GetValue(ColKeyId, index); if (double.IsNaN(value) || double.IsInfinity(value) || (decimal) value != (decimal)... more 11/18/2015 10:00:24 AM

people

Foreach with takeN elements instead of 1

Say I have 23 elements in a list. How can I do foreach taking 5 elements each time and 3 elements the last time? Is there anything like "takeN" method which I can use with foreach...
Jon Skeet
people
quotationmark

There's nothing in the framework, but you can use MoreLINQ's Batch method: foreach (var sublist in myList.Batch(5)) { // sublist has up to 5 elements } (On the final iteration, it will have just 3 elements.) more 11/17/2015 4:47:56 PM

people

'System.Type' does not contain a definition for 'GenericTypeArguments'

As the GenericTypeArguments property was added in .NET 4.5 Is there any way or alternative for this method in .Net 4.0 In my current project I can't use .Net version 4.5. I...
Jon Skeet
people
quotationmark

There's a method Type.GetGenericTypeArguments that has been available for ages. I suspect that at least in most cases, the two are equivalent. I suspect that the property is just part of the reflection API design in .NET 4.5 (to use... more 11/17/2015 2:00:33 PM

people

How to replace values in List<interface>

I want to replace all commas in the display name in a List, how can i do it? for (int i = 0; i < contacts.size(); i++) { ...
Jon Skeet
people
quotationmark

Well you need to set the display name, I suspect. Currently you're calling String.replace which creates a new string - but as strings as immutable, the existing display name won't be changed. I suspect you really want: for (Contact... more 11/16/2015 7:22:52 PM

people

Mono sorts different on different machines

I have a simple sorting issue using mono... If I run the following code on Mac and Windows I get a different result: using System; using System.Linq; public class Program { ...
Jon Skeet
people
quotationmark

This is to be expected, given differences in culture. From the string.CompareTo(string) documentation: This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture. For more information about... more 11/16/2015 1:16:27 PM

people

How can I check if a string has all different characters?

For example, true if the string contains one or more repeated characters. false if the string is composed of all different characters. "A normal string with repeated...
Jon Skeet
people
quotationmark

Think of the string as a sequence of characters. A sequence contain at least one duplicate if the count of the distinct elements is not equal to the overall count of the elements. In other words: bool containsDuplicates =... more 11/14/2015 10:21:00 PM

people

Idempotent modifiers in C#

I noticed that if I write something like: static void Main(string[] args) { const const const bool flag = true; } The compiler doesn't warn me of the multiple consts. So...
Jon Skeet
people
quotationmark

It's a bug in the compiler - at least in Roslyn version 1.0.0.50618. From section 8.5.2 of the C# 5 specification: A local-constant-declaration declares one or more local constants. local-constant-declaration: ... more 11/13/2015 2:43:45 PM

people

Having an issue calling methods from another class

I'm having an issue with calling methods from another class in C#. I haven't been doing this long (3 weeks to be exact) and don't get the ideas behind calling methods. I have...
Jon Skeet
people
quotationmark

You'll currently always see "That is an invalid choice" unless x is 4... because the final if/`else is disconnected from all the rest. You could change it to use else if everywhere, like this: if (x == 1) { ... } else if (x == 2) { ... more 11/13/2015 1:30:52 PM

people