Query Custom Collections in C# with LINQ

Search for a command to run...

No comments yet. Be the first to comment.
A series of articles imported from my previous blog hosted at https://www.talkingitthrough.co.uk.
Often, when building visual applications I find that as I build the applications in stages, there are often times when I’d like to be able to quickly view what data is in the application memory without having to build the UI and link the data to a se...
Introduction Semantic Kernel is an open-source framework by Microsoft that integrates machine learning and natural language processing to build AI applications. It offers tools for tasks like language understanding, text generation, and semantic sear...

Introduction In the earlier articles in my PDF Splitter series, we have relied on the inbuild .NET libraries to handle working with PDF files in MAUI, but these only support opening and displaying of PDF files. Now that we are looking to save our new...

Introduction In the previous article, we created a PDF model and service to allow us to work with and track PDF pages. The next step is to build the UI to allow us to interact with the service. To do this we will create a single 'view' and a 'view mo...

Introduction For the fourth article in my PDF Splitter series, we will start working with PDF files in .NET MAUI. Helpfully, MAUI contains a lot of useful PDF tools for reading PDF files out of the box, so we do not need to use any additional librari...

Introduction In this article, I'm going to introduce basic navigation into my .NET MAUI PDF Splitter application. If you're following my PDF Splitter Application series, you will have a simple application based on the out-the-box .NET template with a...

Language integrated queries are a very simple but powerful way to manage custom collections. Whilst this is only one use for LINQ (it can be used for database connection and all manner of other data driven queries), it is probably the way I use it most frequently.
To demonstrate, I have simply created a custom ‘Person’object:
public class Person
{
public string Forename { get; private set; }
public string Surname { get; private set; }
public int Age { get; private set; }
public Gender Gender { get; private set; }
public Person(string forename, string surname, int age, Gender gender)
{
this.Forename = forename;
this.Surname = surname;
this.Age = age;
this.Gender = gender;
}
}
Then I have created 3 LINQ queries we can apply to a collection of Person objects, one to return only the children:
static IEnumerable GetChildren(IEnumerable personList)
{
IEnumerable children =
from Person in personList
where Person.Age < 18
select Person;
return children;
}
one to return only the women:
static IEnumerable GetWomen(IEnumerable personList)
{
IEnumerable women =
from Person in personList
where Person.Gender == Gender.Female
select Person;
return women;
}
and one to return all members sorted alphabetically by surname:
static IEnumerable OrderAlphabetically(IEnumerable personList)
{
IEnumerable ordered =
from Person in personList
orderby Person.Surname
select Person;
return ordered;
}
You will notice that when we use LINQ here, we’re actually passing and returning collections that implement the IEnumerable interface, of which List is one. LINQ does not return a List by default, so should we need to return a list we can always cast back using .ToList();
To demonstrate the use I have created a quick console application. You may notice that it is also possible to chain queries to apply multiple filters to a list:
class Program
{
static void Main(string\[\] args)
{
List people = new List();
people.Add(new Person("Adam", "Jones", 15, Gender.Male));
people.Add(new Person("James", "Smith", 24, Gender.Male));
people.Add(new Person("Sarah", "Watson", 34, Gender.Female));
people.Add(new Person("Eric", "Johnson", 55, Gender.Male));
people.Add(new Person("Kate", "Davies", 12, Gender.Female));
WriteListToConsole("All people", people);
WriteListToConsole("Children", GetChildren(people));
WriteListToConsole("Women", GetWomen(people));
WriteListToConsole("Female children", GetWomen(GetChildren(people)));
WriteListToConsole("Alphabetically by surname", OrderAlphabetically(people));
Console.WriteLine();
Console.WriteLine("Press any key to exit!");
Console.ReadKey();
}
static void WriteListToConsole(string title, IEnumerable personList)
{
Console.WriteLine();
Console.WriteLine("Members of list: " + title);
foreach (Person p in personList)
{
Console.WriteLine(string.Format("{0} {1}, Age {2}, {3}", p.Forename, p.Surname, p.Age, p.Gender.ToString()));
}
}
//LINQ queries go here, removed for brevity
}

The post Query Custom Collections in C# with LINQ appeared first on Talking I.T. Through.