Thursday, August 15, 2019

Linq predicate builder

A predicate builder can be used to dynamically add conditions to a linq query.

The class is
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
 
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);
  }
}

And it can be used like:

var predicate = PredicateBuilder.False<Product>();

  foreach (string keyword in keywords)
  {
    string temp = keyword;
    predicate = predicate.Or (p => p.Description.Contains (temp));
  }


The complete source code can be found at http://www.albahari.com/nutshell/predicatebuilder.aspx 

Friday, July 26, 2019

Regex to find a string NOT followed by another

Visual studio has slightly different regex syntax. Thus the not symbol is ?! instead of ^ in normal regex.


Lets say we want to find all occurrences of StrA NOT followed by StrB and replace them with StrC

that is given the following data


StrA
StrB
StrAStrB
StrAXYZ
XYZStrA
StrBXYZ

We would like out output to be

StrC         //replaced  StrA since it is not being followed by StrB
StrB
StrAStrB
StrCXYZ      //replaced  StrA since it is not being followed by StrB
XYZStrC      //replaced  StrA since it is not being followed by StrB
StrBXYZ

The regular expression for find is: \b([a-zA-Z0-9_]*)StrA(?!StrB)([a-zA-Z0-9_]*)\b
And for replace is:                          $1StrC$2

TSQL: Convert string in mm:ss.ms format to time

The following statement converts time string (nvarchar) in mm:ss.ms format to Time

print DATEADD(ms, (ISNULL(LEFT(TRY_PARSE(RIGHT(ISNULL(LEFT(RIGHT(@timeString,LEN(@timeString)-CHARINDEX(':',@timeString)), 5), '0'),LEN(ISNULL(LEFT(RIGHT(@timeString,LEN(@timeString)-CHARINDEX(':',@timeString)), 5), '0'))-CHARINDEX('.',ISNULL(LEFT(RIGHT(@timeString,LEN(@timeString)-CHARINDEX(':',@timeString)), 5), '0'))) AS INT), 2), 0)) * 10,
DATEADD(s, ISNULL(TRY_PARSE(LEFT(ISNULL(LEFT(RIGHT(@timeString,LEN(@timeString)-CHARINDEX(':',@timeString)), 5), '0'),LEN(ISNULL(LEFT(RIGHT(@timeString,LEN(@timeString)-CHARINDEX(':',@timeString)), 5), '0'))-CHARINDEX('.',ISNULL(LEFT(RIGHT(@timeString,LEN(@timeString)-CHARINDEX(':',@timeString)), 5), '0'))) AS INT), 0),
DATEADD(mi, ISNULL(TRY_PARSE(LEFT(@timeString,CHARINDEX(':',@timeString)-1) AS INT), 0), '0:0')))

DACPAC deployment error: The database containment option has been changed to None.

This error primarily occurs for Azure SQL databases if we are trying to deploy dacpac package using SqlPackage.exe. It can be resolved by adding the following tag under <PropertyGroup></PropertyGroup>

<Containment>Partial</Containment>

Wednesday, July 10, 2019

List all versions of a nuget package

Use these commands in package manager console to list all versions of a Nuget package

$x = Find-Package UkSport.UKSportHub.Auth2 -AllVersions -ExactMatch
$x.Versions | Format-Table

Friday, June 7, 2019

Publish provider hosted app

The following are steps to publish a provider hosted app in brief:


  1.  Register the app in the site to which it will be added using AppRegNew.aspx
  2. From Visual Studio (VS) generate the app manifest file for the SharePoint Addin Project (not the Remote Web project) by publishing the app to a package file.
  3.  Publish the Remote Web Project to the IIS server
  4.  Add the app to the app catalouge
  5. Add the app to the site collection
We can also view apps using AppPrincipals.aspx and grant them permissions using AppInv.aspx

Regex Email validation in c# dot net core

 Use this regex /^_?[a-zA-Z0-9]([a-zA-Z0-9]*[._+-])*[a-zA-Z0-9_]+@(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.(?!-)[A-Za-z0-9-]{1,63}(?<!-))*\.[A-...