Posts

Showing posts from August, 2020

“NOT IN” equivalent in LINQ in C# (System.Linq)

Image
This blog is going to explain how to achieve ‘NOT IN’ clause output on LINQ using two different ways. Example 1: The following is an example, it has two lists. One is a list of movies, the other is a list of exceptions. In the Where condition !exceptionList.Contains(x) have been checked.  So this will return you a list of movie names that are not on the exception list. List<string> moviesList = new List<string>() { "Tangled", "Brave", "Frozen", "Zootopia", "Moana" }; List<string> exceptionList = new List<string>() {  "Zootopia", "Moana" }; var resultList = moviesList.Where(x=> !exceptionList.Contains(x)).ToList(); foreach (var movie in resultList) { Console.WriteLine(movie); } Output Tangled Brave Frozen Example 2: The following coding is another way to achieve the ‘NOT IN’ function in LINQ. Here the exception() method is used. This method return

Skip & SkipWhile in C# (System.Linq)

Image
This blog is going to explain the Skip() and SkipWhile() method with small examples. Skip() Method From the sequence, the Skip(n) method will skip the first nth element and return the remaining element. If a sequence has five elements, and Skip(2) , it will skip the first two elements and return the remaining three elements. The following figure illustrates how the skip method works. The following is a code example of a Skip() method. The list collection contains 5 movie names, the Skip method skips 2 elements, so it returns the remaining three elements. List<string> moviesList = new List<string>() { "Tangled", "Brave", "Frozen", "Zootopia", "Moana" }; var resultList = moviesList.Skip(2).ToList(); foreach (var movie in resultList) { Console.WriteLine(movie); } Output Frozen Zootopia Moana SkipWhile() Method SkipWhile() skips the sequence of elements as long as the condition is true. If the condition is

Take & TakeWhile in C# (System.Linq)

Image
This blog is going to explain Take() and TakeWhile() with small examples.  Take() metod From the sequence, the Take(n) method will return the first nth element. For example, if there are 5 elements in the list, then Take(2) will get the first 2 elements. The following image illustrates how the Take() method works. The following is the demo code. The moviesList variable has a list of 5 movie names. The take() method take(2) takes the first 2 movie names. IList<string> moviesList = new List<string>() { "Tangled", "Brave", "Frozen", "Zootopia", "Moana" }; var resultList = moviesList.Take(2); foreach (var movie in resultList) {     Console.WriteLine(movie); } Output Tangled Brave TakeWhile() metod The TakeWhile() method returns the elements until the condition is true. In the following example, five movie names are assigned to the moviesList variable. And x != "Zootopia" condition has be

Upload file in ASP.NET Core MVC using jQuery Ajax

Image
This article is going to explain how to upload a file in ASP.NET Core MVC using jQuery Ajax. The following is a controller code. Here IWebHostEnvironment is injected via the HomeController constructor to get the root path. In the FileUpload action method the parameter IFormFile has been passed to upload the files. Note: To save the uploaded file, create a folder name Upload in wwwroot.   using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using System.IO; public class HomeController : Controller {     private readonly IWebHostEnvironment _env;     public HomeController(IWebHostEnvironment hostingEnv)     {         _env = hostingEnv;     }     public IActionResult Index()     {         return View();     }     public async Task<IActionResult> FileUpload(IFormFile formFile)     {         try         {            // To get the physical path of