Category: C#

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

May 29, 2014 at 2:22 amCategory:C# | 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.

Solve:

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

May 28, 2014 at 11:19 amCategory:C# | Entity Framework

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

October 13, 2013 at 10:58 pmCategory:C#

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 ?

e.g.

and within that method you would do:

where CashPayment, ChequePayment etc. are subclasses of Payment.

Linq to object Right outer join

October 9, 2013 at 10:58 pmCategory:C#

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

a,b,c,d,e,f

Try it like this:

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

Working sample: http://ideone.com/MOIhZH


Explanation

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

October 4, 2013 at 10:58 pmCategory:C#

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

October 3, 2013 at 10:58 pmCategory:C#

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

July 18, 2012 at 7:43 amCategory:C#

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

February 27, 2012 at 1:18 amCategory:C#

  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

February 24, 2012 at 1:58 amCategory:C#

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

January 30, 2012 at 1:35 amCategory:C#

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" );

Simple!

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