Posts

Showing posts from February, 2012

Custom LINQ to SQL Query with dynamic result

[TestFixture] public class TestDynamicLinq2Sql { [Test] public void TestDynamicLinq() { var ctx = new MyDataContext(" <connection string> "); var result = ctx.ExecuteQuery(" SELECT USER [User], SUSER_SNAME() [SysUser] "); foreach (dynamic row in result) { Console.WriteLine(" {0},{1} ", row.User, row.SysUser); } } [Test] public void TestDynamicRow() { dynamic d = new DynamicRow(); d.FirstName = " Albert "; d[" LastName "] = " Einstein "; Console.WriteLine(d.FirstName); Console.WriteLine(d.LastName); } } public static class LinqExtensions { public static IEnumerable<dynamic> ExecuteQuery( this DataContext ctx, string query) { using (DbCommand cmd = ctx.Connection.CreateCommand()) { cmd.CommandText = query; ctx.Connecti

Create chart in C# Windows Forms and save it to file

Image
static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault( false ); using (var chartForm = new ChartForm()) { FillData(chartForm.Chart); chartForm.Show(); //save chartForm.Chart.SaveImage(@" Chart.png ", ChartImageFormat.Png); chartForm.Close(); } } public static void FillData(Chart chart) { chart.Titles[0].Text = " Price/Volume Chart "; chart.Series[0].Name = " Price "; chart.Series[1].Name = " Volume "; var random = new Random(); for ( int i = 0; i < 10; i++) { var date = DateTime.Today.AddDays(i); chart.Series[0].Points.Add( new DataPoint(date.ToOADate(), random.NextDouble() * 1e3)); chart.Series[1].Points.Add( new DataPoint(date.ToOADate(), ran

Open Windows Form from nunit test

[Test] public void Test() { var form = new Form1(); Application.Run(form); }

Create excel spreadsheet with C#

Image
* Download EPPlus library http://epplus.codeplex.com/ [Test] public void Test() { var file = @" Sample.xlsx "; if (File.Exists(file)) File.Delete(file); using (var excel = new ExcelPackage( new FileInfo(file))) { var ws = excel.Workbook.Worksheets.Add(" Sheet1 "); ws.Cells[1, 1].Value = " Date "; ws.Cells[1, 2].Value = " Price "; ws.Cells[1, 3].Value = " Volume "; var random = new Random(); for ( int i = 0; i < 10; i++) { ws.Cells[i + 2, 1].Value = DateTime.Today.AddDays(i); ws.Cells[i + 2, 2].Value = random.NextDouble() * 1e3; ws.Cells[i + 2, 3].Value = random.Next() / 1e3; } ws.Cells[2, 1, 11, 1].Style.Numberformat.Format = " dd/MM/yyyy "; ws.Cells[2, 2, 11, 2].Style.Numberformat.Format = " #,##0.000000 "; ws.Cells[2, 3, 11, 3]

SQL Server GROUPING SETS

Image
DECLARE @Products TABLE ( Product varchar (50), Category varchar (50), Price float ) INSERT INTO @Products SELECT ' Sugar ',' Groceries ',2.35 UNION SELECT ' Bread ',' Groceries ',1.09 UNION SELECT ' Ecomonist ',' Magazines ',4.99 UNION SELECT ' FT ',' Magazines ',2.99 SELECT Product,Category, SUM (Price) AS Price, COUNT (*) AS [ Count ] FROM @Products GROUP BY GROUPING SETS((Product),(Category),()) ORDER BY Product DESC , Category DESC , Price DESC , [ Count ] DESC SELECT Product,[Range], SUM (Price) AS Price, AVG (Price) AS AvgPrice FROM ( SELECT Product, Price, [Range] = CASE WHEN Price BETWEEN 0 AND 1 THEN ' 1$ ' WHEN Price BETWEEN 1 AND 2 THEN ' 2$ ' WHEN Price BETWEEN 2 AND 3 THEN ' 3$ ' WHEN Price BETWEEN 3 AND 4 THEN ' 4$ ' WHEN Price BETWEEN 4 AND 5 THEN ' 5$ ' END FROM @Products ) a

C# Compare default(T) values

[TestFixture] public class TestDefaultValue { [Test] public void Test() { Assert.IsTrue(Helper< string >.IsDefault( default ( string ))); Assert.IsTrue(Helper< string >.IsDefault( null )); Assert.IsFalse(Helper< string >.IsDefault(" ")); Assert.IsFalse(Helper< string >.IsDefault(" hello ")); } } public class Helper<T> { public static bool IsDefault(T value ) { if (EqualityComparer<T>.Default.Equals( value , default (T))) { return true ; } return false ; } }