Skip to content

su1c1n1v/LINQ-Sheet-Cheat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LINQ Sheet Cheat

In this project i will show how to work with LINQ

  • To begin i created a class to use as a example and create a List to use the LINQ

Variables

  • Type Users
public class Users
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Job Job { get; set; }

	public Users(int Id, string Name, Job Job)
    {
        this.Job = Job;
        this.Name = Name;
        this.Id = Id;
    }

    public override string ToString()
    {
         return "ID: " + Id + ", Name: " + Name + ", Job: " + Job;
    }
}
  • enum to specify a Job
public enum Job
{
    Developer = 1,
    Driver = 2,
    Jornalist = 3,
    Interviewer = 4,
    Youtuber = 5
}
  • Fill a List of Users
List<Users> list = new List<Users>()
{
    new Users(1,"Vinicius",Job.Developer),
    new Users(2,"Rodrigues",Job.Jornalist),
    new Users(3,"Silva",Job.Developer),
    new Users(4,"Costa",Job.Driver),
    new Users(5,"Vinicius",Job.Youtuber)
};

Select

  • We can Select any attribute of the list, array...
//Select the name of the users
var names = list.Select(Temp => Temp.Name);

//Select more than one attribute
var namesAndJob = list.Select(Temp => new
{
    Temp.Name,
    Temp.Job
});

OrderBy

  • OrderBy Job
    • We can Order a list with any attribute of a class;
//Order the list by Job
list = list.OrderBy(Temp => Temp.Job).ToList();

  • Order Descending by Job
//Order Descending the list by job
list = list.OrderByDescending(Temp => Temp.Job).ToList();

  • Order Descending by Job and Ordered by Name
//When we have two or more entities with the same "name" the LINQ will order these two or more by the second OrderBy.
list = list.OrderByDescending(Temp => Temp.Job).ThenBy(Temp => Temp.Name).ToList();

Where

//Where (Users with Id >= 3)
list = list.Where(Temp => Temp.Id >= 3).ToList();

//Search for the id >= 3 and his job is a developer
list = list.Where(Temp => Temp.Id >= 3 && Temp.Job == Job.Developer).ToList();

Sum

  • Sum a specific collumn and return the result
//Sum of the IDs of the Users
int idSum = list.Sum(Temp => Temp.Id);

//Sum of the IDs of the Users where the ID >= 3
idSum = list.Where(Temp => Temp.Id >= 3).Sum(Temp => Temp.Id);

Any

  • Search in the list if there ara at least one user that match the condicion
//Any User with Id > 3
var r = list.Any(Temp => Temp.Id > 3);

//Exist some user with Id > 9
r = list.Any(Temp => Temp.Id > 9);

Contains

  • Return true or false if a specific user it is in the list
Users usr = list.First(Temp => Temp.Id == 1);
r = list.Contains(usr);

usr = new Users(10, "example", Job.Youtuber);
r = list.Contains(usr);

All

  • Verify if all users in the list match with a specific condicion
//Verify if all users match with the condition (name != hello)
r = list.All(Temp => Temp.Name != "hello");

//Verify if all users match with the condition (Id == 1)
r = list.All(Temp => Temp.Id == 1);

Min and Max

  • Min
    • Min select the min value in a list of elements
//The min ID found in the list
var min = list.Min(Temp => Temp.Id);
  • Max
    • Max select the max value in a list of elements
//The max ID found in the list
var max = list.Max(Temp => Temp.Id);

Distinct

  • The names distinct
    • Return The list of Users with Names distinct
List<string> distintic = list.Select(Temp => Temp.Name).Distinct().ToList();

- We can use the distinct with any attribute in a class or element
- Return The list of Users with jobs distinct
List<Job>  distinticJob = list.Select(Temp => Temp.Job).Distinct().ToList();

Take and TakeWhile

  • Take
    • Take the quantity of users (or elements in a list)
//if we put list.Take(3) the LINQ will return the first three users
var take = list.Take(3);

  • TakeWhile
    • The TakeWhile takes the Users while the condition is "True"
//Temp => Temp.Id < 3
var takeWhile = list.TakeWhile(Temp => Temp.Id < 3);     

First and FirstOrDefault

  • First get the first element in the list
//Return the first element with any paraments
Users first = list.First();
  • FirstOrDefault get the first element in the list but if dont fould anything return null
//Return the first element with the argument "Temp => Temp.Name == "hello""
first = list.FirstOrDefault(Temp => Temp.Name == "hello");
              

Last and LastOrDefault

  • Last get the last element in the list
//Return the last element with any paraments
 Users last = list.Last();
  • LastOrDefault get the last element in the list but if dont fould anything return null
//Return the last element with the parament "Temp => Temp.Name == "hello""
last = list.LastOrDefault(Temp => Temp.Name == "hello");

SequenceEqual

  • SequenceEqual
    • Return bool, if the seguence is equal return true
    • Verify if the list in this and the list in the parament is equal
// Verify if the same list is equal
bool sequence = list.SequenceEqual(list);
// Verify if the list and the list ordered by name is the same
sequence = list.SequenceEqual(list.OrderBy(Temp => Temp.Name));
// Verify if the list and the list ordered by Job is the same
sequence = list.SequenceEqual(list.OrderBy(Temp => Temp.Job));

Credits: Vinícius Costa

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages