I would like to create a list variable of items from another list. So lets say I have a list of 100 items I would like to pull items 25 - 35 and put them inside of another list. is there a way of doing this without calling a big for statement and pulling out the element one by one and putting that into a list.
you can use .Skip and .Take from System.Linq ....
Like this:
var result = myList.Skip(24).Take(10);
and if you need use ToList on the result to get another list
For a List<T>, you can use the GetRange Method.
Creates a shallow copy of a range of elements in the source List(Of
T).
Do note that the second argument represents the count of elements in the range, not the end-index of the range.
Since you mention ArrayList, I should point out that while it too has a GetRange, method, the type is considered essentially legacy since .NET 2.0.
Use both Take and Skip
var newList = oldList.Skip(25).Take(10);
Related
What is the best route to compare 2 List<MyClasses>?
List 2 could contain more values than List 1 which is ok. Just need to make sure List 2 contains everything in List 1. They also won't be in the same order.
You can use Except to find the set difference of two collections.
var hasAllItems = !list2.Except(list1).Any();
The simplest approach is to use Except (in your case, you don't care about the order of your elements in the lists):
var elementsInList2NotInList1 = list2.Except(list1);
Remember that this method uses a default equality comparer to compare the values. If you do care about the ordering of the elements, you'll need to do extra work, in order to check how each element of a list is equal to an element of the other list. The extra work would be the following:
Create your own comparer, implementing the interface IEqualityComparer;
Create your own Equals() and GetHashCode() methods for your custom data type.
One way to do it:
List1.All(o => List2.Contains(o));
This says "return true if and only if every item in list 1 is contained in list 2"
This will let you know if list2 does not contain an item that is in list1:
list1.Any(x => !list2.Contains(x));
You could put it in a variable so you don't have to think about the double negatives:
var list2ContainsList1 = !list1.Any(x => !list2.Contains(x));
The method List<T>.AddRange(IEnumerable<T>) adds a collection of items to the end of the list:
myList.AddRange(moreItems); // Adds moreItems to the end of myList
What is the best way to add a collection of items (as some IEnumerable<T>) to the beginning of the list?
Use InsertRange method:
myList.InsertRange(0, moreItems);
Use InsertRange method:
List<T>.InsertRange(0, yourcollection);
Also look at Insert method which you can add an element in your list with specific index.
Inserts an element into the List at the specified index.
List<T>.Insert(0, T);
List<String> listA=new List<String>{"A","B","C"};
List<String> listB=new List<String>{"p","Q","R"};
listA.InsertRange(0, listB);
Here suppose we have 2 list of string... then using the InsertRange method we can pass the starting index where we want to insert/push the new range(listB) to the existing range(listA)
Hope this clears the code.
Please try List<T>.InsertRange(0, IEnumerable<T>)
Anyone know how to select a certain amount of items in a List to bind to a DataSource? Basically I'm getting back 10 items (which I don't have control over) and I only need to show 5. Originally I was thinking of using a loop and adding 5 items to a new list but that seems like a lot of code. Is there an expression that I can use to select the first 5?
//Returns a List<DataItem>
MyDataListControl.DataSource = Helper.GetDataItems(); //<= Possible expression?
You may take a look at the Skip and Take LINQ extension methods. So in your case if you wanted to take only the first 5 elements of some IEnumerable<T>:
MyDataListControl.DataSource = Helper.GetDataItems().Take(5).ToList();
What about List's GetRange method? Have you tried that? I don't the internal workings of the method; whether it also creates a new list or not.
GetRange(int index, int count)
Here is the msdn link for it.
RemoveRange will probably be best as you won't have to instanciate a new list, unless that happens internally anyway.. Just make sure you're always getting 10 items or you'll potentially get an ArgumentOutOfRangeException.
list.RemoveRange(5, 5);
That should leave you with the first five items.
I've been looking for something like that for days. I'm trying to remove all the elements from a bigger list A according to a list B.
Suppose that I got a general list with 100 elements with differents IDS and I get another list with specific elements with just 10 records. I need remove all the elements from the first list that doesn't exists inside the second list.
I'll try to show the code that I actually don't know how it didnt works.
List<Obj> listA = new List<Obj>();
List<Obj> listB = new List<Obj>();
//here I load my first list with many elements
//here I load my second list with some specific elements
listA.RemoveAll(x => !listB.Contains(x));
I don't know why but it's not working. If I try this example with a List<int> type, it works nicely but I'd like to do that with my object. This object got an ID but I don't know how to use this ID inside the LINQ sentence.
You need to compare the IDs:
listA.RemoveAll(x => !listB.Any(y => y.ID == x.ID));
List(T).RemoveAll
I believe you can use the Except Extension to do this.
var result = listA.Except(listB)
Reference: http://www.dotnetperls.com/except
If you want to remove a list of objects (listB) from another list (listA) use:
listA = listA.Except(listB).ToList()
Remember to use ToList() to convert IEnumerable<Obj> to List<Obj>.
who ever is viewing this now.I think var result = listA.Intersect(listB) will give the result for common values in the both the list.
According to the documentation on MSDN ( http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx ), contains uses the default Equality comparer to determine equality, so you could use IEquatable's Equals method on your Obj class to make it work. HiperiX mentions the ref comparison above.
How to add the IEquateable interface: http://msdn.microsoft.com/en-us/library/ms131190.aspx
Greetings!
I am using List class.I will define my code below.
List<c1> lis = new List<c1>();
where c1 is class.
I want to find index of particular item in List.say in list i have, 10,20,30,40 inside List.In this how i find the index of 30.Please help me to get a solution.
Thank You
Regards
Jennie
Maybe this is what are you looking for? List.IndexOf Method (T)
Use:
lis.IndexOf(value)
That should return the first index of the requested value.
c1 c = new c1(); should be INSIDE the loop.
Actualy, what is your code supposed to represent? Are you aware you have Array inside a list. So in your case, you end up with List with 4 items of same instance of single object, that has array of 4 items with strings "0","1","2","3". You should fix this first before you ask your question again.