The LINQ Question operators are a sequence of continuation strategies that shape a question trend. Those purposes are the extension strategies for both IEnumerable<T> and IQueryable<T> and so they’re outlined within the Gadget.Linq.Queryable and Gadget.Linq.Enumerable predefined libraries,
They shape the construction blocks of LINQ question expressions and are referred to as by means of the use of both static way syntax or example way syntax.
There are round 50 same old question operators to be had in LINQ that come with set operations, concatenation, sorting operators, filtering operators, component operators, aggregation, and so forth. The set operations come with 4 strategies: Distinct, Excluding, Intersect, and Union.
LINQ Distinct Operation With Complicated Sort
Now, what if we upload the identify of scholars to the marks received by means of them above?
Shall we comprehend it thru a code pattern.
create a document named StudentMarks.cs to retailer the complicated checklist.
the use of Gadget;
the use of Gadget.Linq;
the use of Gadget.Collections.Generic;
namespace LINQTutorial
{
public elegance StudentMarks
{
public int Marks { get; set; }
public string Title { get; set; }
public static Record<Scholar> GetStudents()
{
Record<Scholar> scholars = new Record<Scholar>()
{
new Scholar {Marks = 90, Title = “Vaibhav” },
new Scholar {Marks = 67, Title = “Akash” },
new Scholar {Marks = 78, Title = “Anjali” },
new Scholar {Marks = 89, Title = “Rithik” },
new Scholar {Marks = 98, Title = “Harsh” },
new Scholar {Marks = 89, Title = “Anjali” },
new Scholar {Marks = 90, Title = “Akash” },
new Scholar {Marks = 87, Title = “Sameera” },
new Scholar {Marks = 78, Title = “Priya” },
new Scholar {Marks = 82, Title = “Harsh” },
new Scholar {Marks = 90, Title = “Vaibhav” },
new Scholar {Marks = 78, Title = “Anjali” },
};
go back scholars;
}
}
}
We will be able to use this code in the primary way.
the use of Gadget;
the use of Gadget.Linq;
namespace LINQTutorial
{
elegance MainStudent
{
public static void Primary(string[] args)
{
//The use of Manner Syntax
var MS = Scholar.GetStudents()
.Make a choice(std => std.Title)
.Distinct().ToList();
//The use of Question Syntax
var QS = (from std in Scholar.GetStudents(choose std.Title)
.Distinct().ToList();
foreach(var merchandise in MS)
{
Console.WriteLine(merchandise);
}
Console.ReadKey();
}
}
}
Via default, The C# LINQ Distinct serve as will handiest be capable of kind by means of both disposing of repeating scholar names or repeating marks.
The best way to Remedy the Downside?
This downside may also be solved by means of two other approaches.
- Overriding the Equals() and GetHashCode() strategies throughout the Scholar elegance
- The use of IEquatable<T> interface
Manner 1
Create a document named StudentMarks.cs to kind this code.
the use of Gadget.Collections.Generic;
namespace LINQTutorial
{
public elegance StudentMarks
{
public int ID { get; set; }
public string Title { get; set; }
public static Record<Scholar> GetStudents()
{
Record<Scholar> scholars = new Record<Scholar>()
{
new Scholar {Marks = 90, Title = “Vaibhav” },
new Scholar {Marks = 67, Title = “Akash” },
new Scholar {Marks = 78, Title = “Anjali” },
new Scholar {Marks = 89, Title = “Rithik” },
new Scholar {Marks = 98, Title = “Harsh” },
new Scholar {Marks = 89, Title = “Anjali” },
new Scholar {Marks = 90, Title = “Akash” },
new Scholar {Marks = 87, Title = “Sameera” },
new Scholar {Marks = 78, Title = “Priya” },
new Scholar {Marks = 82, Title = “Harsh” },
new Scholar {Marks = 90, Title = “Vaibhav” },
new Scholar {Marks = 78, Title = “Anjali” },
};
go back scholars;
}
public override bool Equals(object obj)
{
//typecasting obj to Scholar Sort
go back this.ID == ((Scholar)obj).ID && this.Title == ((Scholar)obj).Title;
}
public override int GetHashCode()
{
//Getting the ID hash code price
int IDHashCode = this.ID.GetHashCode();
//Getting the string HashCode Price
int NameHashCode = this.Title == null ? 0 : this.Title.GetHashCode();
go back IDHashCode ^ NameHashCode;
}
}
}
Now, we can name the process in the primary serve as thru this code:
the use of Gadget;
the use of Gadget.Linq;
namespace LINQTutorial
{
elegance MainStudent
{
public static void Primary(string[] args)
{
// Manner Syntax
var MS = Scholar.GetStudents()
.Distinct().ToList();
// Question Syntax
var QS = (from std in Scholar.GetStudents()
choose std)
.Distinct().ToList();
foreach (var merchandise in MS)
{
Console.WriteLine($”ID : {merchandise.ID} , Title : {merchandise.Title} “);
}
Console.ReadKey();
}
}
}
Output
Marks = 90, Title = Vaibhav
Marks = 67, Title = Akash
Marks = 78, Title = Anjali
Marks = 89, Title = Rithik
Marks = 98, Title = Harsh
Marks = 87, Title = Sameera
Manner 2
Write this code pattern in a brand new document named StudentMarks.cs.
the use of Gadget.Collections.Generic;
namespace LINQTutorial
{
public elegance StudentMarks : IEquatable<StudentMarks>
{
public int ID { get; set; }
public string Title { get; set; }
public static Record<StudentMarks> GetStudentsMarks()
{
Record<StudentMarks> StudentMarkstudents = new Record<StudentMarks>()
{
new Scholar {Marks = 90, Title = “Vaibhav” },
new Scholar {Marks = 67, Title = “Akash” },
new Scholar {Marks = 78, Title = “Anjali” },
new Scholar {Marks = 89, Title = “Rithik” },
new Scholar {Marks = 98, Title = “Harsh” },
new Scholar {Marks = 89, Title = “Anjali” },
new Scholar {Marks = 90, Title = “Akash” },
new Scholar {Marks = 87, Title = “Sameera” },
new Scholar {Marks = 78, Title = “Priya” },
new Scholar {Marks = 82, Title = “Harsh” },
new Scholar {Marks = 90, Title = “Vaibhav” },
new Scholar {Marks = 78, Title = “Anjali” },
};
go back scholars;
}
public bool Equals(StudentMarks different)
{
if (object.ReferenceEquals(different, null))
{
go back false;
}
if (object.ReferenceEquals(this, different))
{
go back true;
}
go back this.ID.Equals(different.ID) && this.Title.Equals(different.Title);
}
//developing the hashcode serve as
public override int GetHashCode()
{
int IDHashCode = this.ID.GetHashCode();
int NameHashCode = this.Title == null ? 0 : this.Title.GetHashCode();
go back IDHashCode ^ NameHashCode;
}
}
}
Now, paste this code in the primary way.
the use of Gadget;
the use of Gadget.Linq;
namespace LINQTutorial
{
elegance MainStudent
{
static void Primary(string[] args)
{
//Manner Syntax
var MS = StudentMarks.GetStudentsMarks()
.Distinct().ToList();
//Question Syntax
var QS = (from std in StudentMarks.GetStudents()
choose std)
.Distinct().ToList();
foreach (var merchandise in MS)
{
Console.WriteLine($”ID : {merchandise.ID} , Title : {merchandise.Title} “);
}
Console.ReadKey();
}
}
}
Output
Marks = 90, Title = Vaibhav
Marks = 67, Title = Akash
Marks = 78, Title = Anjali
Marks = 89, Title = Rithik
Marks = 98, Title = Harsh
Marks = 87, Title = Sameera
supply: www.simplilearn.com