Let us see how to use Distinct() and FirstOrDefault() clauses with the help of linq.js in .NET Web Application.
Advantage
- It is useful to write simple LINQ queries from Entityframework to client side with LinqJS.
- It’s better for validating data at the client side.
- Improves performance of the application.
Let’s see one by one,
- Distinct() function is different here.
C#.NET Code - The FirstOrDefault() function is nearly similar.
C#.NET CodeHere in defaultWeekData, you will get cmbMonth = “Second week”.
LinqJS Code12345678910111213141516$scope.cmbMonthOfWeek = [{"cmbMonth": "First week","Id": 0}, {"cmbMonth": "Second week","Id": 1}, {"cmbMonth": "Third week","Id": 2}, {"cmbMonth": "Fourth week","Id": 3}, ];var defaultWeekData = Enumerable.From($scope.cmbMonthOfWeek).Where(function(x) {return x.Id == 1}).FirstOrDefault();
With defaultWeekData, you will get cmbMonth = “Second week”.