How to look through arrays? [duplicate] - c#

This question already has answers here:
filter an array in C#
(6 answers)
Closed 5 years ago.
I want a way to look through an array with a certain criteria.
For example,
str[] balance = { hi.Text, bye.Text, hello.Text, what.Text };
How do I look through it with the criteria of "more than 4 characters" so it would look at each one, and skip it if it doesn't meet the criteria.
So like in this example, it would skip hi and bye, but not hello.

var fourOrMore = balance.Where(x => x.Length > 4).ToList();
Would do exactly what you need, you would need to add using System.Linq; to you class file

Related

Overload of "Contains" in C# [duplicate]

This question already has answers here:
What is a method group in C#?
(5 answers)
Closed 4 years ago.
I am using the below code to check if two lists are equal.
var a = ints1.All(ints2.Contains) && ints1.Count == ints2.Count;
The only thing I do not understand is how does ints2.Contains work. As far as I know, Contains() is a method and takes a parameter. As we can see here, Contains is not taking any parameter.
Note - ints1 and ints2 are two different lists.
That's called a method group. It's basically a shorcut for this:
ints1.All(x => ints2.Contains(x))

Transfer content of one array to another, removing duplicates [duplicate]

This question already has answers here:
How do I remove duplicates from a C# array?
(28 answers)
Most efficient way to remove duplicates from a List
(1 answer)
Closed 6 years ago.
I have an array of about 1000 words. I want to transfer them to another array while removing any duplicates. How can I do this in C# ?
The question almost is the answer:
var array2 = array1.Distinct().ToArray();

.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

C# Linq Query to get between items [duplicate]

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.

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