Microsoft Build 2018

So many videos and not enough time for all, here is Build 2018 https://channel9.msdn.com/Events/Build/2018
I will share the ones I watch and note them here.
#build2018

Advertisements

O(1): random access to an…

O(1): random access to an element in a collection, dependent on indexing

O(n): list iterations

O(n^2): nested loops on the same collection

O(log n): divide and conquer

O(n log n): iterations that use divide and conquer

O(n!): adding a nested loop for every input you have
Big-o-complexity-chart

Resource: @DynamicWebPaige

#bigo #big-o

Configuration in .NET Core

#netcore #configuration

Simple self-join with LINQ and SQL

Let’s say we have a table in which we store the various test results of a persons.

PersonId TestId Result
1 TOEFL 5
1 IELTS 10
2 TOEFL 10
2 IELTS 6
3 TOEFL 2
4 IELTS 4

And assuming that it has been queried and stored in a list of objects.

results= db.Query<PersonResult>(SELECT * FROM Results).ToList();

(This is a Dapper query)

To get the result of one person in one row, we can do a self-join with LINQ as following:

var modifiedList = from r1 in theList.Where(x => x.TestId == "IELTS").ToList()
                   join r2 in theList.Where(x => x.TestId == "TOEFL").ToList()
                   r1.PersonId equals r2.PersonId 
                   select new Person
                           {
                                   PersonId= r1.PersonId
                                   TestId = r1.TestId,
                                   ResultToefl= r2.Result
                                   ResultIelts = r1.Resul
                            }

Or another way, I think better is to do is with SQL in the server:

SELECT 
t1.PersonId, t1.Result as TOEFLResult, t2.Items AS IELTSResult
FROM Results t1, Results t2
WHERE t1.PersonId= t2.PersonId
AND t1.TestId = 'TOEFL' and t2.TestID='IELTS'
ORDER BY t1.CompletionDate DESC

#linq #sql #self-join #combine-two-rows-sql #merge-two-row-sql-linq #merge-rows

.NET Core and .NET Standard

A nice article from MSDN Magazine:
.NET Standard – Demystifying .NET Core and .NET Standard
And a great video series about .NET Standard from the same author:
https://www.youtube.com/playlist?list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY

#net-standard
#net-core
#resources

Update Inserted Record with Trigger

There are some cases that you can not change your code if you have an ancient project. If it causes to insert wrong values to db, you may want to validate the value is ok and if not, correct it. We can use db trigger for such cases.


CREATE TRIGGER [dbo].[triggerName]
ON [dbo].[TableName]
FOR INSERT
AS
BEGIN
DECLARE @Name AS varchar(5000)
DECLARE @Category AS varchar(50)
DECLARE @ID AS int
SET @FirstName=(select TestID from inserted)
SET @ID = (select ResultID from inserted)

IF @Category = 'CARS'
BEGIN
SET @Name=(select name from inserted)
IF LEN(@Name) != 50 -- Our condition is the length of the name must be 50 if not, that means it has 0s and we need to replace 0s.
BEGIN
-- 'Lentgth is incorrect:' + Cast(LEN(@Name) as varchar(50))
IF CHARINDEX('0',@Name)>0 -- If name contains any 0s, update those 0s with 'X'  - here we update the table record
BEGIN
PRINT 'Includes Zero'
UPDATE TableName
SET Items = Replace(@Name,'0','X')
WHERE ID = @ID
END
END

END
-- 'IT IS NOT CATEGORY = CARS
END

Important thing here is the type of trigger FOR INSERT

This type runs after each insert operation.

Learn more here:  https://docs.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql

Multiple arguments in SQL IF:

DECLARE @StartDate AS DATETIME
DECLARE @EndDate AS DATETIME

SET @StartDate = NULL
SET @EndDate = NULL

IF (@StartDate IS NOT NULL AND @EndDate IS NOT NULL) 
    BEGIN
        Select 'This works just fine' as Msg
    END
Else
    BEGIN
    Select 'No Lol' as Msg
    END

#sql # mssql #db-trigger, #insert-trigger, #sql-trigger

Azure WebJobs

What is an Azure WebJob?  With WebJobs, you can simply run a program or script just like a running a web app. Azure Functions is also a similar service. You can create a console app and run it continuously or triggered. Let’s try a simple one.

  • Create the console app: dotnet new console
  • Write your code. Console.WriteLine outputs to WebJobs logs page. So you can use that to log simply.
  • Publish the project: dotnet publish -c Release
  • Add a new file with extension .cms, such as Run.cmd in the publish folder – we do this because webjobs do not run dll files directly. It will run this script file.
  • Add the command to run your dll: dotnet YourProject.dll
  • Zip the publish file: YourPorject.zip
  • Follow these steps to upload it to Azure and run it: https://docs.microsoft.com/en-us/azure/app-service/web-sites-create-web-jobs
  • You can use CRON expression for triggered jobs:

Every 15 minutes: 0 */15 * * * *
Every hour (that is, whenever the count of minutes is 0): 0 0 * * * *
Every hour from 9 AM to 5 PM: 0 0 9-17 * * *
At 9:30 AM every day: 0 30 9 * * *
At 9:30 AM every weekday: 0 30 9 * * 1-5

For more advanced features, you can use WebJobs SDK:
https://github.com/Azure/azure-webjobs-sdk

Resources:

WebJobs:
https://docs.microsoft.com/en-us/azure/app-service/web-sites-create-web-jobs

WebJobs vs. Functions:
https://docs.microsoft.com/en-us/azure/azure-functions/functions-compare-logic-apps-ms-flow-webjobs

http://www.hanselman.com/blog/IntroducingWindowsAzureWebJobs.aspx

CRON Expressions Cheat Sheet:
https://codehollow.com/2017/02/azure-functions-time-trigger-cron-cheat-sheet/

#azure, #azure-functions, #azure-webjobs, #webjobs