This question already has answers here:
How does one add a LinkedList<T> to a LinkedList<T> in C#?
(3 answers)
Closed 9 years ago.
Let's say I have the following:
LinkedList<int> list1 = new LinkedList<int>();
LinkedList<int> list2 = new LinkedList<int>();
list1.AddLast(1);
list1.AddLast(2);
list2.AddLast(1);
list2.AddLast(2);
As far as I know you cannot do the following;
list1.AddLast(list2.First);
and except the lists to be connected together.
What is the proper way to merge two LinkedLists in C#? I know there is a Union() method, but it seems like such strong point of LinkedList in C++ is that you can easily combine and break lists apart if need be.
The LinkedList class does not support chaining, splitting, cycles,
or other features that can leave the list in an inconsistent state.
It's equally simple if you use a List instead of a LinkedList. Here are a couple of ways to do the whole list.
LINQ;
var combinedList = list1.Concat(list2).ToList();
Other way I found on msdn;
List<int> combinedList = new List<int>();
combinedList.AddRange(list1);
combinedList.AddRange(list2);
Related
This question already has answers here:
LINQ compare two lists and remove
(4 answers)
Closed 8 months ago.
I am new to c# development. I want to compare and remove list items that already exist in another list. Is there any built in method to do that instead of doing it using loop?
The List object has a RemoveAll method which can remove all elements of the list which match the predicate.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removeall?view=net-6.0
You can also use the except method, answered here: Remove items from one list in another
var list1 = { /* first list */ };
var list2 = { /* second list to remove from first */ };
list1 = list1.Except(list2).ToList();
This question already has answers here:
Linq query and casting in c#
(2 answers)
Closed 10 months ago.
I have a Class, Food which has two derived classes: Meat and Vegetables.
If I have a List of Foods, I can get a (sub)list of Foods that are Meat using
List<Food> allTheFood = GetListOfFood();
List<Food> justTheMeats = allTheFood.Where(x => x is Meat);
But this gives me a list of type Food (where they all happen to be Meat), rather than a list of Meat.
Obviously, I could create a new empty List and then do a foreach and cast every individual item from justTheMeats from Food to Meat and add them into the new List, but that seems a convoluted approach.
Is there a nicer/easier/cleaner way to do this?
Thanks
Yes, use linq OfType
var justTheMeats = allTheFood.OfType<Meat>().ToList();
This question already has answers here:
Is it a good/acceptable practice to declare variable as interface type?
(5 answers)
Using Interface variables
(12 answers)
Closed 4 years ago.
I see this being done so often with ICollection and IEnumberable, seeing new objects being created from the Interface instead of the class itself. To make this simple, let's use IList vs List because I understand those two a lot more.
What is the difference between:
IList<string> People = new List<string>();
vs
List<string> People = new List<string>();
?
Similarly to IEnumerable and ICollections, can't you just use
Collections<string> People = new Collections<string>();
as well instead of "ICollections"? It's so confusing and why is this done?
This question already has answers here:
Use LINQ to get items in one List<>, that are not in another List<>
(11 answers)
Closed 8 years ago.
I have a C# application. I have two lists ('NewStock' and 'OldStock') of the same custom type, 'Book'. I want to extract any 'Book' object that is the list NewStock but not in the OldStock list into a new list called ResultList. Please see an example below. How do I do this?
I have used implemented the IEqualityComparer interface on my class Book so that I check if a book object is equal to another.
NewStock OldStock
A A
B C
C
D
Result I would like,
ResultList
B
D
var results = newStock.Except(oldStock).ToList()
And you can provide a custom comparer to Except to do this.
Use except to do this:
Example can be found here:
MSDN
Try this one:
List<Book> ResultList = NewStock.Select(x=>!OldStock.Contains(x)).ToList();
This question already has answers here:
IEnumerable vs List - What to Use? How do they work?
(11 answers)
IList vs IEnumerable for Collections on Entities
(2 answers)
Closed 9 years ago.
I am confused. Can anybody help me to understand Difference between IEnumeration<T> instead and List<T>?
You mean IEnumerable<T>. It's the base interface of all collection types like arrays or generic List<T>.
You can for example create a List<int>:
List<int> ints = new List<int>(){ 1,2,3 };
But since it implements IEnumerable<T> you could also declare it in this way:
IEnumerable<int> ints = new List<int>(){ 1,2,3 };
This has the advantage that you cannot modify ints since Remove comes from ICollection<T>.