LINQ EXAMPLES. LINQ is probably one of the main reasons why I’ve stuck with C#. It’s such a powerful in memory data manipulation namespace in dotNet and the main reason why you can abbreviate tens of lines of code into one.
I’ve accumulated a few queries which I use regularly which I hope you may find useful and which I know I’ll be referring to again.
The examples below are based on the following class Model
and the following xml
Importing XML into a List of T
This code takes the above xml file and converts it into a List
XDocument xDoc = XDocument.Load(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), “AdvancedSearchesCsvs.xml”));
var Csvs = xDoc
.Select(r => new ModelCsv
Csv = r.Element(“CsvName”).Value,
RCode = r.Attribute(“ReadCode”).Value
Parsing a DataSet into a List of T
Lets say you have created a dataset from a SQL Query and want to parse it into a List
In this case the Dataset has a column called “CsvName” and a column called “ReadCode”.
var Csvs = ds.Tables.AsEnumerable().Select(r => new ModelCsv
Csv = r.Field(“CsvName”),
RCode = r.Field(“ReadCode”)
Changing the value of a property in a List of T
There are times when you want to just change the value of one of the properties in a List of Objects without having to unwravel it, change the property then bundle it up again
Csvs is your List and you would like to change the value of RCode to another value (which you could have in a foreach). In this example
Csvs.Where(s => s.RCode == “READCODE TO CHANGE”).Select(r =>
r.RCode = “CHANGED READCODE VALUE”;
Convert a List of T into a Dictionary
This stumped me for a little while but got it in the end. The key is to create a anonymous object then assign the properties of this object to the Key and Value of the Dictionary.
In this example the List is a List
var csvDictionary = new Dictionary<string, string>(Csvs.Select(l => new
CsvName = l.Csv,
ReadCode = l.RCode
}).ToDictionary(k => k.CsvName, k => k.ReadCode));
Reorder an List of T removing duplicates of one property
Useful if you want to reorganise your List of T to only have a unique value in one property which is different to .Distinct() which will only create unique entries of the objects container.
In this example you already have a List of modelCsvs
modelCsvs = modelCsvs.GroupBy(r => r.ReadCode)
.Select(g => g.FirstOrDefault())
.OrderBy(r => r.PatientID)
Comparing 2 lists with one shared property
Sometimes you have 2 lists and you want to select objects which property values between both.
For example lets say we have another List of Patients with ReadCodes as one of their values which we will call ExternalPatients and we want to know which of these patients are already on our original List of ModelCsvs.
This is similar to a List.Contains() Method but allows you to select a property of an object instead. List.Contains() can only be used with simple types.
ExternalPatients.Any(e => e.ReadCode == m.ReadCode))
Now we have a little repository of examples which you might find useful on your travels.