Tuesday, January 3, 2023

SingleOrDefault vs FirstOrDefault

 SingleOrDefault returns a SINGLE element or null if no element is found. If 2 elements are found in your Enumerable then it throws the exception you are seeing. Just like Highlander... with Single - there can be only one.

FirstOrDefault returns the FIRST element it finds or null if no element is found. so if there are 2 elements that match your predicate the second one is ignored.

deliveryGroup.DeliveryMethods.SingleOrDefault(x => x.Id == methodId)

deliveryGroup rd = this.db.Details .FirstOrDefault(x => x.TId == Id && x.TypeId == TypeId);


its use very careful because when found any duplicate SingleOrDefault it will throw error.

Convert json date in jQuery

 The date format js is give in the below section, please go below and see the date.format.js and copy and use it.

Table of content for JSON date format

  1. JSON date
  2. Parse JSON date to date
  3. Date format types
  4. Demo link for live output
  5. JavaScript code for date format
  6. Full example code for multiple date format

 The JSON date is
        /Date(1297246301973)/

The JQuery Code is
        var jsonDate = "\/Date(1297246301973)\/";
        var date = new Date(parseInt(jsonDate.substr(6)));

Sunday, February 21, 2021

Tuple in C#

Tuple Tuple has max limit up to 7 permeameter.

The Tuple<T> class was presented in .NET Framework 4.0. A tuple is an information structure that contains an arrangement of components of various information types. It very well may be utilized where you need to have an information construction to hold an article with properties, however you would prefer not to make a different sort for it. 

Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>
Example:
Tuple<int, string, string> per = 
                        new Tuple <int, string, string>(1, "Darshan", "Mitesh");
How to access tuple:
var person = Tuple.Create(1, "Darshan", "Jobs");
person.Item1; // returns 1
person.Item2; // returns "Darshan"
person.Item3; // returns "Jobs"

var numbers = Tuple.Create("One", 2, 3, "Four", 5, "Six", 7, 8);
numbers.Item1; // returns "One"
numbers.Item2; // returns 2
numbers.Item3; // returns 3
numbers.Item4; // returns "Four"
numbers.Item5; // returns 5
numbers.Item6; // returns "Six"
numbers.Item7; // returns 7
numbers.Rest; // returns (8)

Monday, December 9, 2019

C# 8.0 new features :



1) Default Interface Methods

interface IWriteLine 

 public void WriteLine() 
 { 
 Console.WriteLine("Wow C# 8!"); 
 } 


2) Nullable reference type

string? nullableString = null; 

Console.WriteLine(nullableString.Length);

3) Advanced Pattern Matching

Example
var point = new 3DPoint(1, 2, 3); //x=1, y=2, z=3 
if (point is 3DPoint(1, var myY, _)) 

  // Code here will be executed only if the point .X == 1, myY is a new variable 
  // that can be used in this scope. 

}

4) Async streams

await foreach (var x in enumerable) 

  Console.WriteLine(x); 

}

5) Ranges

Index i1 = 3; // number 3 from beginning 
Index i2 = ^4; // number 4 from end 
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 

Console.WriteLine($"{a[i1]}, {a[i2]}"); // "3, 6" 

6) Caller Expression Attribute

public static class Verify { 
    public static void InRange(int argument, int low, int high, 
        [CallerArgumentExpression("argument")] string argumentExpression = null, 
        [CallerArgumentExpression("low")] string lowExpression = null, 
        [CallerArgumentExpression("high")] string highExpression = null) { 
        if (argument < low) { 
            throw new ArgumentOutOfRangeException(paramName: argumentExpression, message: $ " {argumentExpression} ({argument}) cannot be less than {lowExpression} ({low})."); 
        } 
        if (argument > high) { 
            throw new ArgumentOutOfRangeException(paramName: argumentExpression, message: $ "{argumentExpression} ({argument}) cannot be greater than {highExpression} ({high})."); 
        } 
    } 
    public static void NotNull < T > (T argument, 
        [CallerArgumentExpression("argument")] string argumentExpression = null) 
    where T: class { 
        if (argument == null) throw new ArgumentNullException(paramName: argumentExpression); 
    } 

// CallerArgumentExpression: convert the expressions to a string!   
Verify.NotNull(array); // paramName: "array"   
// paramName: "index"   
// Error message by wrong Index:   
"index (-1) cannot be less than 0 (0).", or 
// "index (6) cannot be greater than array.Length - 1 (5)."   

Verify.InRange(index, 0, array.Length - 1);

7) Default in deconstruction

(int x, string y) = (default, default); // C# 7 

(int x, string y) = default;               // C# 8

8) Using declarations

// C# Oldy Style 
using (var repository = new Repository())   
{   
} // repository is disposed here!   
   
// vs.C# 8   
   
using var repository = new Repository();   
Console.WriteLine(repository.First());   

// repository is disposed here! 

9) Generic attributes

public class GenericAttribute<T> : Attribute { } 
public class ValidatesAttribute<T> : Attribute {} 
[Validates<string>] 
public class StringValidation {} 
[Validates<int>] 

public class IntegerValidation{} 

10) Static Local Functions

int AddFiveAndSeven() 

  int y = 5; int x = 7; 
  return Add(x, y); 
  static int Add(int left, int right) => left + right; 

Sunday, September 8, 2019

Test-driven development (TDD)

TDD is development methodology.  by TDD Your code will make sure there are no bugs.

Requirements are turned into very specific test cases, then the software is improved so that the tests pass.

TDD is a software development process that relies on the repetition of a very short development cycle.

Image result for tdd development methodology

Benefits of TDD:

Writing the tests first requires you to really consider what do you want from the code.

You receive fast feedback.

TDD creates a detailed specification.

TDD reduces time spent on rework.

Spend less time in the debugger.

Able to identify the errors and problems quickly.

TDD tells you whether your last change (or refactoring) broke previously working code.

TDD allows the design to evolve and adapt to your changing understanding of the problem.

TDD forces the radical simplification of the code. You will only write code in response to the requirements of the tests.

You're forced to write small classes focused on one thing.

The resulting unit tests are simple and act as documentation for the code.

The development time to market is shorter.

The programmer’s productivity is increased.

Quality is improved.

Bugs are reduced.






Thursday, August 22, 2019

Map network drive with sql

exec xp_cmdshell 'net use Z: \\192.01.01.10\FileStorage [Password] /user:192.01.01.10\Administrator'(Username)

--exec xp_cmdshell 'net use Z: /delete'

--exec xp_cmdshell 'dir Z:'

For more ref : https://www.mytechmantra.com/LearnSQLServer/Configure-Network-Drive-Visible-for-SQL-Server-During-Backup-and-Restore-Using-SSMS/

Tuesday, December 5, 2017

Visual Studio 2017 download

Visual Studio Community 2017

A free, fully featured, and extensible solution for individual developers to create applications for Android, iOS, Windows, and the web.
Download

Visual Studio Enterprise 2017

An integrated, end-to-end solution for developers looking for high productivity and seamless coordination across teams of any size.
Download

Visual Studio Professional 2017

Improve productivity with professional developer tools and services to build applications for any platform.
Download

Visual Studio Test Professional 2017

Drive quality and collaboration throughout the development process. 
Download

Visual Studio Team Explorer 2017

A free solution for non-developers to interact with Team Foundation Server and Visual Studio Team Services.
Download 

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...