To find the duplicate values only:var duplicates = list. GroupBy(x => x. Key). Where(g => g.
How to remove the duplicates in the list using linq? You can also do var set = new HashSet<int>(); var uniques = items. Where(x => set. Add(x.Id)); .
Linq, acts upon 2 collections. It returns a new collection that contains the elements that are found. Union removes duplicates. So this method can be thought of as two actions: it combines the two collections and then uses Distinct() on them, removing duplicate elements.
Possible Duplicate:
LINQ: Select parsed int, if string was parseable to int
This could be a basic question, but I couldn't figure out a work around. I have an array of strings and I tried to parse them with integers. As expected I got Format Exception.
How could I skip "3a" and proceed parsing the remaining array and storing the integers into output using Linq.? Is this a better approach or a DON'T DO practice? Pls shed some light on how to use TryParse in this case
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] values = { "1", "2", "3a","4" };
List<int> output = new List<int>();
try{
output = values.Select(i => int.Parse(i)).ToList<int>();
}
catch(FormatException)
{
foreach (int i in output)
Console.WriteLine(i);
}
foreach (int i in output)
Console.WriteLine(i);
Console.ReadLine();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With