C# extension method similar to Sql’s IN operator

Consider the code snippet below

if(a == x || a == y || a == z)

We can re-write this expression as:

  • Using array’s contains

 if( new [] {x,y,z}.Contains(a))

  • Using a similar syntax for the SQL’s IN operator by using extension

public bool IsIn(this T obj, params T[] collection) {
   return collection.Contains(obj);
}
And invoke as
if(a.IsIn(b, c, d)) { … }

Read more at Stackoverflow

Find the Port a Connection is Using in Sql Server

SELECT c.session_id, c.local_tcp_port, s.login_name, s.host_name, s.program_name
FROM sys.dm_exec_connections AS c INNER JOIN
            sys.dm_exec_sessions AS s on c.session_id = s.session_id
WHERE c.local_tcp_port 1433

I found this very useful when monitoring external access to your Sql Server instance.

The original post was here at SqlServerCentral