instagram youtube
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
logo
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

What’s C# Linq Distinct? Defined With Examples

- Team

Jumat, 26 Juli 2024 - 19:45

facebook twitter whatsapp telegram line copy

URL berhasil dicopy

facebook icon twitter icon whatsapp icon telegram icon line icon copy

URL berhasil dicopy


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.

Desire a Best Device Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

The best way to Remedy the Downside?

This downside may also be solved by means of two other approaches.

  1. Overriding the Equals() and GetHashCode() strategies throughout the Scholar elegance
  2. 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

Be told 15+ In-Call for Equipment and Talents!

Automation Checking out Masters ProgramDiscover Program

Learn 15+ In-Demand Tools and Skills!

supply: www.simplilearn.com

Berita Terkait

Most sensible Recommended Engineering Tactics | 2025
Unfastened Flow Vs General Flow
Be told How AI Automation Is Evolving in 2025
What Is a PHP Compiler & The best way to use it?
Best Leadership Books You Should Read in 2024
Best JavaScript Examples You Must Try in 2025
How to Choose the Right Free Course for the Best Value of Time Spent
What Is Product Design? Definition & Key Principles
Berita ini 7 kali dibaca

Berita Terkait

Selasa, 11 Februari 2025 - 22:32

Revo Uninstaller Pro 5.3.5

Selasa, 11 Februari 2025 - 22:21

Rhinoceros 8.15.25019.13001

Selasa, 11 Februari 2025 - 22:12

Robin YouTube Video Downloader Pro 6.11.10

Selasa, 11 Februari 2025 - 22:08

RoboDK 5.9.0.25039

Selasa, 11 Februari 2025 - 22:05

RoboTask 10.2.2

Selasa, 11 Februari 2025 - 21:18

Room Arranger 10.0.1.714 / 9.6.2.625

Selasa, 11 Februari 2025 - 17:14

Team11 v1.0.2 – Fantasy Cricket App

Selasa, 11 Februari 2025 - 16:20

Sandboxie 1.15.6 / Classic 5.70.6

Berita Terbaru

Headline

Revo Uninstaller Pro 5.3.5

Selasa, 11 Feb 2025 - 22:32

Headline

Rhinoceros 8.15.25019.13001

Selasa, 11 Feb 2025 - 22:21

Headline

Robin YouTube Video Downloader Pro 6.11.10

Selasa, 11 Feb 2025 - 22:12

Headline

RoboDK 5.9.0.25039

Selasa, 11 Feb 2025 - 22:08

Headline

RoboTask 10.2.2

Selasa, 11 Feb 2025 - 22:05