Category Archives: C#

Add ID value for non-auto-increament field under Entity Framework

If you use an “ID” field in your entity, & this “ID” field is not auto-increament, Entity Framework will fail when generate insert sql.


Add a DatabaseGeneratedAttribute to this “ID” field, with DatabaseGeneratedOption.None option, like this:

Successfully solved under Entity Framework 4-6.

Entity Framework 6.0 MigrationHistory table on SQLite

The following SQL is used to create __MigrationHistory table in SQLite database for Entity Framework 6.0 in Code-First mode.


Instantiate particular class based on object value

I have classes for each payment mode for e.g. Cash, Cheque, Card. I have to pass object as a parameter based on object value I have to instantiate a relevant class.

How can I achieve this? Suggest me a better design

You could pass an enumeration (Cash/Cheque/Card) into a factory ?


and within that method you would do:

where CashPayment, ChequePayment etc. are subclasses of Payment.

Linq to object Right outer join

I have written following linq query yet I am not getting expected result.My Expected result is all the matching record bettween lst & list & all the non matching record from list
for example.

I want following result


Try it like this:

result is (a,b,c,d,e,f)

Working sample:


To make a left/right join in linq you have to use DefaultIfEmpty method, that will yield (default) result when there is no match (the joined result is empty). However, default value for string is null so you have to provide default value from the “left side” collection to see it in the result set.

Alternative approach

This is probably more convenient approach. Instead of selecting from z & providing the default value, you will select from x.address – the left side of the join.

use parentheses in c# calculator

Hello I just write a simple calculator in C# & I want to improve my program to handle parentheses.

Here is my button to add 1(digit):

This is a method for my Plus button:

And this is for to calculate final result:

Anyone can assist me to write parentheses for my program?

You can use NCalc – Mathematical Expressions Evaluator for .NET

C# Get generic non-array type from generic array type

Given the following function;

This works fine, yet sometimes the function fails before T passed is an array type, yet it mustn’t be an array type. These functions have to do with JSON deserialization of a dictionary, yet for some reason it doesn’t accept the T array argument when the dictionary has only one entry.

In short, I want to do this

I’ve tried a ton of stuff, & I realize the real problem is somewhere else, yet I need to temporary fix this so I can work on a real solution in the deserializer. I tried reflection too using the following method;

But that doesn’t quite work either.

Thank you!

I am not really sure what you are trying to achieve here, yet to obtain the type of the elements in an array, you have to use Type.GetElementType():

Setup visual style for WPF applications

Maybe you’re facing the same problem with me: WPF applications cannot use Windows Visual Style by default.
If you want a solution, follow these steps:

  1. Download this & unpack: Link.
  2. Add the “application.manifest” file to your wpf project in VisualStudio.
  3. Right click on the project in “Solution Explorer”, select “Properties”.
  4. Choose “Application” tab, in the “Resource” groupbox, select “Icon & manifest”, then select “application.manifest” on manifest combobox.
  5. Save all & build application, run it!


Attributes related to WinForms Designer

  1. BrowsableAttribute
    This attribute tells designer, whether the associated property should be displayed.
  2. DefaultEventAttribute
    This attribute tells designer, if double clicked a component, which event it should generate code for.
  3. DefaultValueAttribute
    This attribute indicates the default value for the property. If you donate a value not equals default value, it will be displayed in bold.
  4. DesignerCategoryAttribute
    This attribute tells designer it belongs to what category.
  5. DesignerSerializationVisibilityAttribute
    This attribute tells designer how to serialize the associated property.
  6. DesignTimeVisibleAttribute
    This attribute tells designer whether the component should be displayed in toolbox.

To be continued…

How to use default parameter in C# 2.0 & 3.5

For example, I need a method like this:

public void InitData(int index, object value, bool required = false) {}

I can write it as:

The attribute Optional tells compiler that the parameter required has default value. And the attribute DefaultParameterValue(false) tells compiler what is the default value.

Very simple. Isn’t it?

WPF in action: Dynamically set style for control

For example, I have a xaml like this:

<TabItem Header="Tab1" Style="{DynamicResource TabItemWithButton}">

I want create a new tab dynamically, & set style to “TabItemWithButton”, I will use this code:

newTab.SetResourceReference( TabItem.StyleProperty, "TabItemWithButton" );


Actually, you can use SetResourceReference method to set many dependency properties, explore it!