problem
|
|
c# dispose objects of Dictionary
you cannot dispose the object of a Dictionary with "foreach" process, you can do with "while" getting the Enumerator of the Dictionary. |
|
difficulty level
|
|
5/10 :|
|
|
compatibility
|
|
c#
|
|
solution
|
|
In the follow example, I scan serially a
Dictionary generic of <string, IInDataObject> type and I dispose its
hosted objects.
Each object in the dictionary, each Value is IInDataObject interface. This interface implements IDisposable so can call Dispose(); //get the Enumerator in order to scan with while (and not with foreach) System.Collections.IEnumerator enumerator = dataObjectList.GetEnumerator(); //perform the Enumerator in while loop while (enumerator.MoveNext()) { //get the pair of Dictionary KeyValuePair<string, IInDataObject> pair = ((KeyValuePair<string, IInDataObject>)(enumerator.Current)); //dispose it pair.Value.Dispose(); } |
Developing with CSharp many time you feel a strange feeling... then feeling of lost and how to solve it. Fortunately MSDN has done a good work to help the developers. .net offers thousand ways to do several things! This is the worst thing… because, you do not know where to start from and what works in real world! I hope to help you with your CSharpache.
Wednesday, 30 November 2011
c# how to dispose objects of Dictionary
Tuesday, 29 November 2011
csharp, process the items of Dictionary serial and faster
The Dictionary type abstracts out its looping logic into enumerators:
these are accessed through the foreach loop and the GetEnumerator method.
In this article, we demonstrate the GetEnumerator method,
which exhibits better performance than the foreach loop.
Finally
Examples
Let's examine the most common and easiest way to loop through a Dictionary instance. The foreach loop here actually compiles into intermediate language that uses the GetEnumerator method, MoveNext, and Current, as well as a try/finally block.Finally
Looping over Dictionary with foreach [C#] static int A(Dictionary<string, int> d) { int a = 0; foreach (var pair in d) { a += pair.Value; } return a; }Next, this method demonstrates the GetEnumerator and MoveNext methods and the Current property directly. This code is compiled to the same intermediate language except the try/finally block is absent.
Looping over Dictionary with GetEnumerator [C#] static int B(Dictionary<string, int> d) { int b = 0; var enumerator = d.GetEnumerator(); while (enumerator.MoveNext()) { var pair = enumerator.Current; b += pair.Value; } return b; }
Monday, 28 November 2011
c# - how to move a project to different file location
- Get backup of the Directory with Project (you don't know what
Microsoft can do for you)
- From Visual Studio remove the project for Solution
- If you use Source safe do Check in the pendings (ctrl-wg). This step is important if you use Source safe!
- Move the directory of the Project to the new directory location you desire.
- Remove all *.*scc files from all directories of the project. Some of them might be hidden and read only, so pay attention.
- Right click on Solution and click on Add -> Existing project
- Navigate the dialog and open the .csproj file you placed in new file location
- If you use the Safe Source you might get the misleading message "The associated source control plug-in is not installed or vould not me initialized". Select "Temporarily work uncontrolled" an click ok.
- If you use Source safe do Check in the pendings (ctrl-wg). This step is important if you use Source safe!
- Rebuild your solution and your will get error because this Project is not referenced any more to other Project that was before we removed it. So add this Project in References of the other project where use it.
- From Visual Studio remove the project for Solution
- If you use Source safe do Check in the pendings (ctrl-wg). This step is important if you use Source safe!
- Move the directory of the Project to the new directory location you desire.
- Remove all *.*scc files from all directories of the project. Some of them might be hidden and read only, so pay attention.
- Right click on Solution and click on Add -> Existing project
- Navigate the dialog and open the .csproj file you placed in new file location
- If you use the Safe Source you might get the misleading message "The associated source control plug-in is not installed or vould not me initialized". Select "Temporarily work uncontrolled" an click ok.
- If you use Source safe do Check in the pendings (ctrl-wg). This step is important if you use Source safe!
- Rebuild your solution and your will get error because this Project is not referenced any more to other Project that was before we removed it. So add this Project in References of the other project where use it.
Thursday, 24 November 2011
mssql - compare two datetime fields
problem
|
|
mssql - compare
two datetime
Mssql, compare two datetime fields, if their values are close, if are in 24h difference and do on --- > dennis |
|
difficulty level
|
|
1/10 :)))
|
|
compatibility
|
|
mssql
|
|
solution
|
|
In mssql, the
datetime fields are actually numeric with decimals. The integer part in the
“days”. So if you want to compare two dates compare the integer part as
bellow
-- compare if the item is sold 24h before the item is modified Select * from items Where ModificationDate<SaleDate-1 -- find person(s) that born on 12/11/2010 (not exactly on the same, with at least 12 hours difference) Select * from persons Where birthdate-0.5>=’2010-12-11’ and birthdate+0.5<=’2010-12-11’ |
Wednesday, 2 November 2011
MSSQL - how to copy tables and data
problem
|
|
MSSQL - how to copy
- How to copy a table’s structure to another database? - How to create a table with the same structure (fields) on the - How to copy the data of the table to another table? |
|
difficulty level
|
|
4/10 :)
|
|
compatibility
|
|
general
|
|
solution
|
|
How to copy a table’s
structure to another database?
The follow code, not only copy’s the structure but also copies and the records of it. You may filter the data applying a “where” as I do here. --Do like: SELECT * INTO [TargetDatabase].[dbo].[MyTable] FROM [SourceDatabase].[dbo].[MyTable] where [SourceDatabase].[dbo].[MyTable].CustomerCode='01'; -- where is optional How to create a table with the same structure (fields) on the same database? (Like you did previously!) --Do like: SELECT * INTO MyNewTable FROM MySourceTable where MySourceTable.CustomerCode='01'; -- where is optional How to copy the data of the table to another table? --Syntax: INSERT INTO TargetTable( <field list> ) SELECT <field list> FROM SourceTable --Do like: INSERT INTO NewCustomers ( Company, Branch, Name ) SELECT Company, Branch, Name FROM Customers Where Branch=’01’; -- where is optional |
Subscribe to:
Posts (Atom)