Get part of the list using LINQ [duplicate] - c#

This question already has answers here:
Subset of Array in C#
(9 answers)
Closed 1 year ago.
Say, I have a list and I want to get a portion of it. Say I have 1000 data from 0 to 999. Then I want to get from "index1" to "index2."
Sample data is :
[0] = 100
[1] = 1520
....
[900] = 8975
....
[998] = 10
[999] = 4875
Say for example I want to get values from index 900 to index 998. So the value return should be:
[0] = 8975
.....
[998] = 10
How to do that in LINQ?

You can use skip and take for that
List<int> list= new List<int>;
list.Skip(900).Take(100);
https://www.codingame.com/playgrounds/213/using-c-linq---a-practical-overview/skip-and-take
Or, you can use the GetRange method
List<int> list= new List<int>;
list.GetRange(900, 100); // Retrieves 100 items starting with index #900
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.getrange?redirectedfrom=MSDN&view=net-6.0#System_Collections_Generic_List_1_GetRange_System_Int32_System_Int32_

Related

Combine to array sorts into one? [duplicate]

This question already has answers here:
How can I sort a List<T> by multiple T.attributes?
(3 answers)
Closed 4 years ago.
I was wondering if it is possible to combine my sorting code into one ?
I sort my array firstly by enum, then by an id number from lowest to highest.
So my current code looks like this:
_entities = _entities.OrderBy(a => a.Priority).ToArray(); //sort by enum
_entities = _entities.OrderBy(a => a.PriorityID).ToArray(); //then sort by id
So the enum has:
High, Medium,Low
And IDs are just integers.
So the end result should be like:
High¬
0 , 1 ,2
Medium¬
0, 1, 2
Low¬
0, 1, 2
I don't know the syntax to combine these, or if this is as simple as it gets? But it does seem a bit inefficient to be sorting by each property one by one.
Whats the correct way to do ths?
You can use ThenBy:
_entities = _entities.OrderBy(a => a.Priority).ThenBy(a => a.PriorityID).ToArray();

C# List Logic Fail [duplicate]

This question already has answers here:
How to Sort a List<T> by a property in the object
(23 answers)
Closed 6 years ago.
i got an output from a list into an excel file. To simplefile things it could look like this:
4
1
2
3
Now all i wanna do is, put it in the right order:
1
2
3
4
which i did with this code ( isSort contains 4 1 2 3 ):
...
var isSortFin = new List<Item>();
var FirstElement = isSort.First();
foreach (var Itemd in toSort)
{
if (Itemd.Summary != FirstElement.Summary)
{
isSortFin.Add(Itemd);
}
}
isSortFin.Add(FirstElement);
return isSortFin;
now my output is:
3 2 1 4
and not the aspired:
1 2 3 4
where do i go wrong?
You could use OrderBy linq extension and sort element.
isSort= isSort.OrderBy(x=>x).ToList();
If Item is an object, specify property name in OrderBy expression to sort on that property.
//ex..
isSort= isSort.OrderBy(x=>x.Value).ToList();

C# Linq, Find highest number from list using linq [duplicate]

This question already has answers here:
Maximum integer value find in list<int>
(7 answers)
Closed 8 years ago.
I have below list, how do i find the highest or max number.
List<int> numbers = new List<int>();
numbers.Add(2);
numbers.Add(5);
numbers.Add(7);
numbers.Add(10);
Please help!
you just need to use Max method of list.
i.e :
var result = numbers.Max();
Use Max:-
int highestNum = numbers.Max();

I want count uniques along with value in array? [duplicate]

This question already has answers here:
How to Count Duplicates in List with LINQ
(7 answers)
Closed 9 years ago.
I have 6 numbers in array .
string[] list = { "1", "1", "2","2","1","3" };
I want result like this. please help.
"1" = 3
"2" = 2
"3" = 1
var itemCounts = list.GroupBy(l => l)
.Select(g => new { key = g.Key, count = g.Count()});
Assuming your numbers in SearchArray >0. Here is an alternative approach
You can also write a function
1) find Max - One Loop
for( int i=0;i<searchArray.length;i++){
if (searchArray[i]>max) max=searchArray[i];
}
2) Initialize an Array[Max+1]= 0
3) Loop thru each item and increment the size in Array
for( int i=0;i<searchArray.length;i++){
Array[searchArray[i]]++;
}

How to replace value in list at same collection location [duplicate]

This question already has answers here:
How to replace list item in best way
(12 answers)
Closed 5 years ago.
How do I replace a value in a collection list at the same location?
0 = cat
1 = dog
2 = bird
replace 2 with snail?
Do you mean:
yourCollection[2] = "Snail";
In a bigger List<T> collection, you would like to find index to replace...with:
var i = animals.FindIndex(x => x == "Dog");
animals[i] = "Snail";
List<string> animals = new List<string>();
animals.Add("cat");
animals.Add("dog");
animals.Add("bird");
animals.Add("fish");
animals.RemoveAt(2);
animals.Insert(2, "snail");

Categories

Resources