Assign Short Cut Key to a button in WPF

This is kind of old, but I ran into the same issue today. I found that the simplest solution is to just use an AccessText element for the button’s content. <Button Command=”{Binding SomeCommand}”> <AccessText>_Help</AccessText> </Button> When you press the Alt key, the ‘H’ will be underlined on the button. When you press the key combination … Read more

When should you use the as keyword in C#

Use as when it’s valid for an object not to be of the type that you want, and you want to act differently if it is. For example, in somewhat pseudo-code: foreach (Control control in foo) { // Do something with every control… ContainerControl container = control as ContainerControl; if (container != null) { ApplyToChildren(container); … Read more

Initializer syntax: new ViewDataDictionary { { “Name”, “Value” } }

ViewDataDictionary implements IDictionary<string, object>. IDictionary<string, object> is essentially a collection of KeyValuePair<string, object>. Your ViewDataDictionary initializer (outer curly braces) contains another set of curly braces that represents a KeyValuePair<string, object> initializer. The reason this is possible is explained in this answer. You can Add multiple items by comma separating the KeyValuePair<string, object> initializers: var data … Read more

Simple Examples of joining 2 and 3 table using lambda expression

Code for joining 3 tables is: var list = dc.Orders. Join(dc.Order_Details, o => o.OrderID, od => od.OrderID, (o, od) => new { OrderID = o.OrderID, OrderDate = o.OrderDate, ShipName = o.ShipName, Quantity = od.Quantity, UnitPrice = od.UnitPrice, ProductID = od.ProductID }).Join(dc.Products, a => a.ProductID, p => p.ProductID, (a, p) => new { OrderID = a.OrderID, … Read more

Multiplying all values in IEnumerable

What you’re looking for is the Aggregate function int modValue = ints.Aggregate(1, (x,y) => x * y); The Aggregate function takes in an initial accumulator value and then applies an operation to every value in the enumeration creating a new accumulator value. Here we start with 1 and then multiply ever value by the current … Read more

When using object initializers, why does the compiler generate an extra local variable?

Thread-safety and atomicity. First, consider this line of code: MyObject foo = new MyObject { Name = “foo”, Value = 42 }; Anybody reading that statement might reasonably assume that the construction of the foo object will be atomic. Before the assignment the object doesn’t exist at all. Once the assignment has completed the object … Read more

Creating a Month Dropdown in C# ASP.NET MVC

In your view model you could have a Months property: public IEnumerable<SelectListItem> Months { get { return DateTimeFormatInfo .InvariantInfo .MonthNames .Select((monthName, index) => new SelectListItem { Value = (index + 1).ToString(), Text = monthName }); } } which could be directly bound to a DropDownListFor: <%= Html.DropDownListFor(x => x.SelectedMonth, Model.Months) %>

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