Friday, September 18, 2015

Build Where Clause Dynamically Using Linq PredicateBuilder to filter data from DataTable C#

To filter some data from a DataTable based on two or more condition is not possible using the below code. As the Linq where condition not allowed more than one condition.
var filtered = dataTable.Where(x => x["Name"].Equals("Venkat") && x => x["Age"].Equals("26")).ToList()

So using the PredicateBuilder we can build where clause dynamically. Check the below code.,

As you can see, the below code is constructing the where clause dynamically. You can pass multiple column name and value as filter condition dynamically.
var predicate = PredicateBuilder.True();
predicate = predicate.And(x => x["Name"].Equals("Venkat"));
predicate = predicate.And(x => x["Age"].Equals("26"));
predicate.Compile();
var filtered = dataTable.Where(predicate).ToList();

public static class PredicateBuilder
{
    public static Expression<Func<T, bool>> True<T>() 
    { 
        return f => true; 
    }

    public static Expression<Func<T, bool>> False<T>() 
    { 
        return f => false; 
    }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>> (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>> (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
    }
}

Friday, September 11, 2015

How to convert CSV to EXCEL using C#

It is possible using the EPPLUS .net library. It is free and available in epplus.codeplex.com. You have to pass both CSV and the EXCEL file path where it should be saved. The ExcelTextFormat class has more conditions, based on that it reads the CSV file. Check the below console application sample code that accomplish that task.
static void Main(string[] args)
{
 string csvFilePath = @"D:\sample.csv";
 string excelFilePath = @"D:\sample.xls";

 string worksheetsName = "TEST";
 bool firstRowIsHeader = false;

 var excelTextFormat = new ExcelTextFormat();
 excelTextFormat.Delimiter = ',';
 excelTextFormat.EOL = "\r";

 var excelFileInfo = new FileInfo(excelFilePath);
 var csvFileInfo = new FileInfo(csvFilePath);

 using (ExcelPackage package = new ExcelPackage(excelFileInfo))
 {
  ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(worksheetsName);
  worksheet.Cells["A1"].LoadFromText(csvFileInfo, excelTextFormat, OfficeOpenXml.Table.TableStyles.Medium25, firstRowIsHeader);
  package.Save();
 }

 Console.WriteLine("Converted!");
 Console.ReadLine();
}
EPPLUS Setup

The EPPLUS can be installed through Visual Studio Nuget package also. Follow the below steps to install it.

  • Right click the Visual Studio solution and select "Manage NuGet Packages for Solution".
  • And in the window search for EPPlus. 
  • From the searched list select EPPlus and click install.

Wednesday, September 9, 2015

How to use Sass/Scss in Visual Studio

Visual Studio update 2 has the native Scss support. So we can write Scss code but, to compile it, we need Sass compilers. Those are available as Visual Studio extensions.

There are many extensions available in the internet. You can use Web Workbench extension as it is very simple to use. Using Web Workbench extension in visual studio, files are compiled automatically to CSS with the same name whenever you save it.

So you should refer the compiled CSS files to the project to make the CSS work.

Folow the below steps to install the Web Workbench extension in Visual Studio,
  • Download the extension from here
  • Double click to Install it
  • Restart the Visual Studio
  • You will see a new MINDSCAPE menu in Visual Studio.
Reference: http://www.mindscapehq.com/products/web-workbench