How to update a property of an object in LINQ without returning new objects.

If you want to update an property of an object in LINQ without creating new objects, you can use the following code: using System; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Assert = Xunit.Assert; namespace Research.Rli.Tests { public class Appointment { public int Id { get; set; } public string …

How to find and get a specific element from a generic list or collection with LINQ and C#

If you want to find or get an element from a generic list or collection with LINQ and C#, you can use the "First" function: public ApplicationPool GetApplicationPoolByName(string name) { ApplicationPool applicationPool; using (ServerManager manager = new ServerManager()) { applicationPool = manager.ApplicationPools.First(a => a.Name == name); } return applicationPool; } …

How to sum TimeSpan in LINQ with C#

If you want to sum TimeSpan properties in LINQ with C#, use: List<TimeSpan> list = new List<TimeSpan>             { new TimeSpan(1), new TimeSpan(2), new TimeSpan(3) }; // TimeSpan.Zero is the initial offset, in this case 0 ticks // subtotal is used to sum to items in the list // t is …

How to get all items from a list that are not unique with LINQ and C#

If you want to get all items from a list that are not unique with LINQ and C#, you can use: using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using NUnit.Framework.SyntaxHelpers; namespace Ada.Cdf.Test { [TestFixture] public class IntegrationTester { [Test] [Explicit("Not a unittest")] public void Test() { List<string> …

How to count items per category with LINQ and C#

If you want to count items per category with LINQ in C#, you van use: using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using NUnit.Framework.SyntaxHelpers; namespace Ada.Cdf.Test { [TestFixture] public class IntegrationTester { [Test] [Explicit("Not a unittest")] public void Test() { List<string> items = new List<string>(); items.Add("Item 1"); …

Get index of an item with LINQ and C#

If you want to use the index of an item with LINQ in C#, you can use the “index” statement: using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using NUnit.Framework.SyntaxHelpers; namespace Ada.Cdf.Test { [TestFixture] public class IntegrationTester { [Test] [Explicit("Not a unittest")] public void Test() { List<string> items …