C# Linq Query to get between items [duplicate] - c#

This question already has answers here:
Get previous and next item in a IEnumerable using LINQ
(11 answers)
Closed 9 years ago.
Hi I have a dictionary like this
("ABC","X")
("CDE","C")
("EFG","X")
I need to retrieve the items which are in two sides of "C" , How to achieve this with linq ?
Output Expected :
Item = n / output = (n-1)(n+1) items
("ABC","X") and ("EFG","X")

Something like this?
Dict.TakeWhile(e => e.Value = "C").Take(1).Reverse().Take(3).
Then remove the middle one.

Related

How do I delete a specific list of lines from a text document? [duplicate]

This question already has answers here:
How can I efficiently remove elements by index from a very large list?
(6 answers)
Remove oldest n Items from List using C#
(6 answers)
Remove all but the first item in a list
(2 answers)
How to remove selected array values in C#
(5 answers)
Move First 10 List Items to Another Item List
(5 answers)
Closed 1 year ago.
This is what I have but there isn't anything that talks about how to delete multiple specific lines, for example, from line 2 - 8.
var file = new List<string>(System.IO.File.ReadAllLines(lsbMatches.SelectedItem.ToString()));//location
file.RemoveAt(2);// i want to change this to multiple lines
File.WriteAllLines(lsbMatches.SelectedItem.ToString(), file.ToArray());//location
You can use RemoveRange because you have converted your text file into a list of strings.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removerange?view=net-5.0
You're best option is to do :
file.RemoveRange(2, 6);

Comparing two lists distinct value C# [duplicate]

This question already has answers here:
How to merge 2 List<T> and removing duplicate values from it in C#
(5 answers)
Closed 6 years ago.
I have 2 lists say list1 and list2. Like
List<int> list1 = {1,2,3};
List<int> list2 = {3,4};
now I want my resultant list to be like
{1,2,3,4}
I have tried this
result= list1.Union(list2).ToList();
But it gives result like
{1,2,3,3,4}
Can anyone help me with this?
It's simple:
var result= list1.Union(list2).Distinct().ToList();

.NET list access index [duplicate]

This question already has answers here:
Is the order of elements on a C# List<T> deterministic?
(3 answers)
Closed 8 years ago.
Follow up question from How to select List<> by its index and return the content?
Can someone please comment if the elements in the list are expected to retain order? In the example above, is "Belly Buster" always expected to be at index 1 or are there cases where that might not be true?
List<string> pizzas = new List<string>();
pizzas.Add("Angus Steakhouse");
pizzas.Add("Belly Buster");
pizzas.Add("Pizza Bianca");
pizzas.Add("Classic Cheese");
pizzas.Add("Friday Special");
string result = pizzas[1]; // result is equal to "Belly Buster"
index starts at 0 so yes if the code stays the same..belly buster will always be at 1

how to select elements that not intersec? [duplicate]

This question already has answers here:
Get the symmetric difference from generic lists
(7 answers)
The opposite of Intersect()
(8 answers)
Closed 9 years ago.
If I have two list and I want the elements that are common in both lists, I can use this code:
var listC = listA.Intersect(listB);
However, If I want the elements that are not common? And without duplicates? is possible with intersect?
Thanks.
Neither answer so far will include items from listB that aren't in listA. To get any item that is in either list, but is not in both lists:
listA.Union(listB).Except(listA.Intersect(listB));
Yep, that's possible. It's called Enumerable.Except.
Use this:
var result = listA.Except(listB); //maybe a .ToList() at the end,
//or passing an IEqualityComparer<T> if you want a different equality comparison.
Most efficient:
var set = new HashSet<T>(listA);
set.SymmetricExceptWith(listB);

C# remove elements in list starting with # [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to remove elements from an array
I have an List with a bunch of data in it. Some of the lines start with a #. I want to remove those lines.
How...?
assuming its a string List
myList.RemoveAll(x => x.BeginsWith("#"));

Categories

Resources