Expression.GreaterThan fails if one operand is nullable type, other is non-nullable

The problem here is that the expression library is throwing an exception when given two arguments of mismatched nullability. Here’s a simple repro: Expression<Func<DateTime?>> ex1 = ()=>DateTime.Now; Expression<Func<DateTime>> ex2 = ()=>DateTime.Now; var ex3 = Expression.GreaterThan(ex1.Body, ex2.Body); It is not clear to me whether this is a bug or not; the rules of C# require that … Read more

StringBuilder Vs StringWriter/StringReader

What is the possible use of StringWriter and StringReader, which cannot be done by StringBuilder itself? StringReader and StringWriter derive from TextReader and TextWriter respectively. So what they can do act as a TextReader or TextWriter instance, which string or StringBuilder cannot because they do not derive either of those types. What is the practical … Read more

Lambda for Dummies….anyone, anyone? I think not

Let’s dissect your code sample: filenames.SelectMany(f => Assembly.LoadFrom(f).GetCustomAttributes(typeof(PluginClassAttribute), true) .Cast<PluginClassAttribute>() .Select(a => a.PluginType) ).ToList(); So, we start off with a string[] called filenames. We invoke the SelectMany extension method on the array, and then we invoke ToList on the result: filenames.SelectMany( … ).ToList(); SelectMany takes a delegate as parameter, in this case the delegate must … Read more

Recursive control search with LINQ

Take the type/ID checking out of the recursion, so just have a “give me all the controls, recursively” method, e.g. public static IEnumerable<Control> GetAllControls(this Control parent) { foreach (Control control in parent.Controls) { yield return control; foreach(Control descendant in control.GetAllControls()) { yield return descendant; } } } That’s somewhat inefficient (in terms of creating lots … Read more

How to OpenWebConfiguration with physical path?

You will have to map the physicalPath to a virtualPath. Here is how you would do that. using System.Web.Configuration; //Reference the System.Web DLL (project needs to be using .Net 4.0 full, not client framework) public static Configuration OpenConfigFile(string configPath) { var configFile = new FileInfo(configPath); var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name); var wcfm = … Read more

How to split string into a dictionary

It can be done using LINQ ToDictionary() extension method: string s1 = “(colorIndex=3)(font.family=Helvicta)(font.bold=1)”; string[] t = s1.Split(new[] { ‘(‘, ‘)’ }, StringSplitOptions.RemoveEmptyEntries); Dictionary<string, string> dictionary = t.ToDictionary(s => s.Split(‘=’)[0], s => s.Split(‘=’)[1]); EDIT: The same result can be achieved without splitting twice: Dictionary<string, string> dictionary = t.Select(item => item.Split(‘=’)).ToDictionary(s => s[0], s => s[1]);

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)