@manhnguyenv

Welcome to my blog!

IdentityServer4 and Asp.Net Core Identity

February 10, 2020 21:35

IdentityServer4 and Asp.Net Core Identity (edit)

https://github.com/skoruba/IdentityServer4.Admin

This web application is based on these projects:

  • ASP.NET Core
  • IdentityServer4.EntityFramework
  • ASP.NET Core Identity
  • XUnit
  • Fluent Assertions
  • Bogus
  • AutoMapper
  • Serilog

Any feedback is welcome - feel free to create an issue or send me an email - jan@skoruba.com.

Microsoft REST API Guidelines

https://github.com/Microsoft/aspnet-api-versioning/tree/master/samples/aspnetcore/SwaggerSample

https://github.com/microsoft/aspnet-api-versioning

https://dotnetcoretutorials.com/2017/01/17/api-versioning-asp-net-core/

ASP Core 2.2 Web API + Api Versioning + Swagger (HAY)

https://github.com/jobairkhan/MultipleApiVersionsWithSwagger/tree/AspNetCore2.2

ASP.NET Core 2.1 Web API + Api Versioning + Swagger (HAY)

https://dejanstojanovic.net/aspnet/2018/november/setting-up-swagger-to-support-versioned-api-endpoints-in-aspnet-core/

https://github.com/dejanstojanovic/api-versioning-swagger

ASP.NET Core 2.1 WEB API

https://www.talkingdotnet.com/create-petstore-like-swagger-ui-asp-net-core-web-api/

Add Swagger to ASP.NET Core 2.1 Web API include PetStore sample UI

https://github.com/talkingdotnet/ASPNETCoreSwaggerDemo

Upload File using Angular 5 and ASP.NET Core 2.1 WEB API

https://github.com/talkingdotnet/Angular5FileUpload/

Add JWT Bearer Authorization to Swagger and ASP.NET Core

https://dotnetcoretutorials.com/2017/05/18/csrf-tokens-angularjsjquery-asp-net-core/

https://ppolyzos.com/2017/10/30/add-jwt-bearer-authorization-to-swagger-and-asp-net-core/

https://ssandhu.co.uk/wordpress/2019/03/01/asp-net-core-2-1/

https://medium.com/@salmanlone89/add-swagger-to-asp-net-core-2-1-web-api-f5ef0d170d4f

https://medium.com/@salmanlone89/customize-swagger-to-asp-net-core-2-1-web-api-8af4ff28835d

ASP.NET Core MVC application using API, OpenID Connect Hybrid flow , second API, Code Flow with PKCE

Updated to .NET Core 3.1

https://github.com/damienbod/AspNetCoreHybridFlowWithApi

ASP.NET Core 2.1 with CRUD in A LIST (List replace with Database)

https://github.com/prateekparallel/StudentRegistrationDemo3

LINQ - Populate object collections from multiple sources

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-populate-object-collections-from-multiple-sources-linq

Generic Database Access

https://www.codeproject.com/Articles/601307/Generic-Database-Access

https://www.codeguru.com/columns/dotnet/step-by-step-guide-to-using-generic-ado.net.htm

 

List & Array

December 18, 2019 00:10

List & Array (edit)

 

Algorithms

Intersection of Two Unsorted Arrays

http://www.codinghelmet.com/exercises/array-intersection

http://www.codinghelmet.com/exercises/counting-intersection-of-two-unsorted-arrays

References

Intersection of two arrays in C#

https://stackoverflow.com/questions/20687338/intersection-of-two-int-array-in-c-sharp

https://stackoverflow.com/questions/10866756/fast-intersection-of-two-sorted-integer-arrays

Merge two array

https://stackoverflow.com/questions/59217/merging-two-arrays-in-net?rq=1

Sort dictionary

https://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value?rq=1

Pass an array of integers to ASP.NET Web API

https://stackoverflow.com/questions/9981330/pass-an-array-of-integers-to-asp-net-web-api?rq=1

EF Core 2.1 select/distinct

var resulat = from a in A
                  join b in B equals a.level=b.level
                  where ...
                  select new M1 {AId = a.id};

    (from r in resulat
    join c in C equals r.AId = c.AId
    select new M2 
    {
      CId = c.Id
      level = c.level
    }).Distinct();

https://stackoverflow.com/questions/54061471/ef-core-2-1-select-distinct

EF Core 2.1

https://entityframeworkcore.com/knowledge-base/51234185/translate-sql-query-into-entity-framework-core-2-1

https://entityframeworkcore.com/knowledge-base/54468767/selecting-all-rows-based-on-a-distinct-column-in-entity-framework-core

Distinct

List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
IEnumerable<
int> distinctAges = ages.AsQueryable().Distinct();
Console.WriteLine(
"Distinct ages:");
foreach (int age in distinctAges)
Console.WriteLine(age);
/* This code produces the following output: Distinct ages: 21 46 55 17 */

GroupBy Example

var directors = new[] { new { DirectorName = "Director A", DirectorID = 1 },
                        new { DirectorName = "Director B", DirectorID = 2 }};
var movies = new[] { new { MovieName = "Movie A", MovieID = 1, DirectorID = 1 },
                     new { MovieName = "Movie B", MovieID = 2, DirectorID = 2 }};
var actors = new[] { new { ActorName = "Actor A", ActorID = 1, MovieID = 1},
                     new { ActorName = "Actor B", ActorID = 2, MovieID = 1},
                     new { ActorName = "Actor C", ActorID = 3, MovieID = 1},
                     new { ActorName = "Actor D", ActorID = 4, MovieID = 2}};

var results = from d in directors
              from m in movies
                .Where(m => m.DirectorID == d.DirectorID)
              from a in actors
                .Where(a => a.MovieID == m.MovieID)
              where d.DirectorID == 1
              group new { d, m, a } by d.DirectorName into grp
              select new 
              { DirectorName = grp.Key,
                MovieCount = grp.Select(x => x.m).Distinct().Count(),
                ActorCount = grp.Select(x => x.a).Distinct().Count()
              };

GroupBy

https://stackoverflow.com/questions/57673111/ef-core-2-1-evaluates-locally-when-sum-complex-and-grouping?rq=1

return query
   .GroupBy(e => new // Key
   { 
       e.DivisionCode,
       e.DivisionDescription,
       e.TopDivisionCode,
       e.TopDivisionDescription,
       e.PostingDate
   },
   e => new // Element
   {
       e.LineAmount,
       RUCAmount = e.LineAmount - (e.Quantity * e.UnitCostLcy) // <--
   })
  .Select(g => new V_TurnoverByDivision
  {
      DivisionCode = g.Key.DivisionCode,
      DivisionDescription = g.Key.DivisionDescription,
      TopDivisionCode = g.Key.TopDivisionCode,
      TopDivisionDescription = g.Key.TopDivisionDescription,
      PostingDate = g.Key.PostingDate,
      LineAmount = g.Sum(e => e.LineAmount),
      RUCAmount = g.Sum(e => e.RUCAmount) // <--
  }); 

Entity Framework Core 2.1 How to OrderBy() and Distinct()

return await _context.SdrSettingHistory
    .Where(x => x.StartDate != null)
    .Select(x => x.StartDate.Value.Year)
    .Distinct()
    .OrderBy(x => x)
    .ToListAsync();

https://stackoverflow.com/questions/52677846/entity-framework-core-2-1-how-to-orderby-and-distinct

Programming: Intersect Two Arrays C#

Input: Array 1: [90,95,136,137], Array 2: [95,135,136,137]

Output: Array INTERSECT: [95,136,137], Array DELETE: [90], Array NEW: [135]

static void Main(string[] args)
{
List<int> lstIDInDB = new List<int>()
{
90,95,136,137
};

List<int> lstIDInModel = new List<int>()
{
95,135,136,137
};

//var arrKeep = (from x in lstIDInDB
// join y in lstIDInModel on x equals y
// select x)
// .OrderBy(x => x)
// .ToList();

var arrKeep = lstIDInDB.Intersect(lstIDInModel).ToList();

Console.WriteLine("Items will be Keep:");

//Keep
for (int i = 0; i < arrKeep.Count; i++)
{
Console.WriteLine(arrKeep[i]);
}

//var arrDeleteAll = (from x in lstIDInDB
// join y in lstIDInModel on x equals y into z
// from t in z.DefaultIfEmpty()
// select x)
// .OrderBy(x => x)
// .ToList();
//var arrDelete = arrDeleteAll.Except(arrKeep).ToList();

var arrDelete = lstIDInDB.Except(arrKeep).ToList();

Console.WriteLine("Items will be Delete:");
//Delete
for (int i = 0; i < arrDelete.Count; i++)
{
Console.WriteLine(arrDelete[i]);
}

//var arrNewAll = (from x in lstIDInModel
// join y in lstIDInDB on x equals y into z
// from t in z.DefaultIfEmpty()
// select x)
// .OrderBy(x => x)
// .ToList();
//var arrNew = arrNewAll.Except(arrKeep).ToList();

var arrNew = lstIDInModel.Except(arrKeep).ToList();
Console.WriteLine("Items will be New:");
//New
for (int i = 0; i < arrNew.Count; i++)
{
Console.WriteLine(arrNew[i]);
}
}

How to mock the DataContext EF & DataContext Linq

August 13, 2018 22:45

https://www.gaui.is/how-to-mock-the-datacontext-entity-framework/

https://www.gaui.is/how-to-mock-the-datacontext-linq/

Linq good articles

March 2, 2018 10:04

LINQ

http://www.linqhealth.co/

Linq to JavaScript

https://toidicodedao.com/2015/04/14/ap-dung-linq-trong-javascript-chuyen-nhieu-nguoi-chua-biet/

https://toidicodedao.com/tag/linq/

Linq to SQL

https://msdn.microsoft.com/en-us/library/bb308959.aspx

https://msdn.microsoft.com/en-us/library/bb425822.aspx

https://msdn.microsoft.com/library/308e66ac-f704-4e00-9b4e-7af0045a2374

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/downloading-sample-databases

Inner Join, Cross Join and Left Outer Join With LINQ to SQL

October 25, 2017 16:55

The join operations in this article are done using COURSE and STUDENT tables. So now I am going to explain the joins to be done using LINQ to SQL step-by-step.

Step 1: Create COURSE and STUDENT Tables in the database as in the following:
 
 CREATE TABLE COURSE
 
(
     course_id int IDENTITY(1,1) PRIMARY KEY,
 
    course_name nvarchar(70) NOT NULL,
 
    course_desc nvarchar(255) NULL,
 
    modified_date date NULL,
 
)
 CREATE TABLE STUDENT
 
(
 
    student_id int IDENTITY(1,1) PRIMARY KEY,
 
    student_name nvarchar(70),
 
    student_city nvarchar(30),
 
    course_id int NOT NULL
 
)
 

Step 2: Define foreign key constraints on the STUDENT table as in the following:
 
 ALTER TABLE STUDENT
 
ADD CONSTRAINT [FK_STUDENT_COURSE] FOREIGN KEY (course_id)REFERENCES COURSE(course_id)
 

Step 3: Create Data Layer
 
Create a dbml file (Operation.dbml in this article) and using the Object Relation Designer create a data context class for the STUDENT and COUSRE tables.

Create-Data-Layer.gif
 
In the above figure we drag and drop both the COURSE and STUDENT tables onto the designer map of the Operation.dbml file and that shows the relationship between the COURSE and STUDENT tables.
 
Step 4: Inner Join in LINQ to SQL
 
 OperationDataContext odDataContext = new OperationDataContext();
 var studentInfo = from student in odDataContext.STUDENTs
 join course in odDataContext.COURSEs
 on student.course_id equals       course.course_id
 select new { student.student_name, student.student_city, course.course_name, course.course_desc };
 

In the above code we join the STUDENT table and the COURSE table using the "join" keyword and using the "on" keyword join the tables by the course_id field of both tables. It returns all rows with a common course_id in both tables.
 
Step 5: Cross Join in LINQ to SQL
 
 OperationDataContext odDataContext = new OperationDataContext();
 var studentInfo = from student in odDataContext.STUDENTs
 from course in odDataContext.COURSEs
 select new { student.student_name, student.student_city, course.course_name, course.course_desc };
 

In the above code we are doing a cross-join on both the STUDENT table and the COURSE table. We get all rows from both tables and the total rows are STUDENT table rows * COURSE table rows.
 
Step 6: Left Join in LINQ to SQL
 
 OperationDataContext odDataContext = new OperationDataContext();
 var courseInfo = from course in odDataContext.COURSEs
 join student in odDataContext.STUDENTs
 on course.course_id equals  student.course_id into studentInfo
 from students in studentInfo.DefaultIfEmpty()
 select new 
 {
     STUDENTNAME = (students.student_name == null)? NULL":students.student_name, 
     STUDENTCITY = (students.student_city == null)? "NULL":students.student_city,
     COURSENAME = course.course_name, 
     COUSREDESCRIPTION = course.course_desc 
 };
 

In above code we are doing a Left Join. Here the left table is COURSE. So it will return all rows from the course table not depends STUDENT table course_id field. If the course_id field value is in the COURSE table but not in the STUDENT table then in the row course_name and course_desc fields will show and student_name and student_city fields will show NULL. Here the output will be the total number of rows in the COURSE table.

LINQ

October 13, 2017 13:12

Source

foreach (var item in searchFilterItems)
{
    if (item.Rows == null || item.Rows.Count <= 0) continue;
    foreach (var child in item.Rows)
    {
        if (child == null || !child.IsSelected) continue;
        var newItem = new SearchConditionItem
        {
            AuditLocationId = item.Id,
            Id = child.Id
        };
        returnList.Add(newItem);
    }
}

Target

returnList.AddRange(from item in searchFilterItems
where item.Rows != null && item.Rows.Count > 0
from child in item.Rows
where child != null && child.IsSelected
select new SearchConditionItem
{
    AuditLocationId = item.Id,
    Id = child.Id
});

Categories

Recent posts