Is there a Linq method to 'extend' collection? - c#

I wonder if there is a Linq method to simply enumerate over another collection after the current one? If not, why not?
To highlight exactly what I mean, imagine we had:
List<int> a = new List<int> { 1, 2, 3 };
int[] b = { 1, 5, 6 };
then I am asking if there is a Linq method such that a.MyHypotheticalExtensionMethod(b) would produce the IEnumerable containing: {1,2,3,1,5,6}
Of course, its trivial to roll one's own (or even to just work around) but it definately seems like something that ought to be included in Linq?

You're looking for a.Concat(b).

Related

Sort and IList of numbers in ascending order c#

So I came across over this problem which I thought is quite easy but got me thinking.
The task is to sort an Ilist of numbers in ascending order.
As far as I understood we can't use Sort() method for Ilists, since it is not build in the intreface.
Could you please help me what would be the best and simple solution to sort an Ilist?
IList<int> list = new List<int>() { -5, 8, -7, 0, 44, 121, -7 };
You can simply use Linq for the task:
var list = new List<int>() { -5, 8, -7, 0, 44, 121, -7 };
var sorted = list.OrderBy(x => x);
If you want to do an in-place sort, you can use ArrayList.Adapter()
As per the documentation:
The ArrayList class provides generic Reverse, BinarySearch and Sort
methods. This wrapper can be a means to use those methods on IList;
however, performing these generic operations through the wrapper might
be less efficient than operations applied directly on the IList.
if you know the unterlying type of your IList you can cast to List and use Sort method.
((List<int)list).Sort()

C# Enumerable.Except() only returns the same element once [duplicate]

This question already has answers here:
Except has similar effect to Distinct?
(3 answers)
Closed 6 years ago.
When running the code below, I would expect to see the number 3, two times, but apparently the Except() method only returns the same element once.
List<int> x = new List<int>() {1, 2, 3, 3};
List<int> y = new List<int>() { 1, 2 };
var z = x.Except(y); /* returns 3, only once */
In the documentation they say that the set difference of two sets is defined as the members of the first set that do not appear in the second set. It is not documented that they return duplicate items only once.
https://msdn.microsoft.com/en-us/library/bb300779(v=vs.110).aspx
Is this a bug, or do I miss something here?
The code of the Expect() method is available here: https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,e289e6c98881b2b8.
Shouldn't they do "if (!set.Contains(element)) yield return element;" instead of "if (set.Add(element)) yield return element;"?
Apparently, this is not a bug; set difference here means that both sequences are treated as sets, consequently the result sequence contains each element only once. However, the documentation does not really enlarge on whether doubles can occur in the output or not.

Comparing two lists and deleting identical results c#

I have two lists:
List<int> positionsThatCannotBeMovedTo =...
List<int> desiredLocations =...
I am trying to remove all of the positions which cannot be moved to from the desired locations to create a list of safe positions:
List<int> safePositions = new List<int>(uniquePositions);
safePositions.RemoveAll(positionsThatCannotBeMovedTo);
however it's throwing the error:
"Argument1: cannot convert from 'System.Collections.Generic.List' to 'System.Predicate'
I'm not entirely sure what this means or how I'm misusing the function. Is anybody able to explain this for me please? I am doing it this way because of the answer in this question:
Compare two lists for updates, deletions and additions
RemoveAll takes a Predicate<T>, but you are passing a list:
safePositions.RemoveAll(x => positionsThatCannotBeMovedTo.Contains(x));
There is another way to obtain a list with elements except the elements of another list
List<int> positionsThatCannotBeMovedTo = new List<int>() {1,2,3,4,5,6,7};
List<int> uniquePositions = new List<int>() {5,6,7,8,9,10};
List<int> safePosition = uniquePositions.Except(positionsThatCannotBeMovedTo).ToList();
MSDN on Enumerable<T>.Except
You could also accomplish this using the Except extension method. Assuming uniquePositions is your list of all your positions.
var safePositions = uniquePositions.Except(positionsThatCannotBeMovedTo).ToList();
Except is the set difference operator and as you are using lists of ints the default comparer is fine.

How to remove each item that appears in first collection from second collection using linq?

Suppose I have 2 collections:
1) {1, 2, 3, 5}
2) {2, 5}
I want to remove each item that appears in second collection from first collection, so I will get:
{1, 3}
Questions:
How can I do this with Join OP (better with extension methods syntax)?
And is there any way I can iterate over two collections as I do with nested for/foreach loops?
Edits:
To iterate over two collections simultaneously You can use nested from clauses:
from boy in boys
from girl in girls
select boy + "+" + girl
First time I found this syntax necessary :)
This does what you need
var solution = list1.Except(list2);
You can find more details about Except here, but the basic idea is:
This method returns those elements in first that do not appear in
second. It does not also return those elements in second that do not
appear in first.
Easy. Use Except
var newList = list1.Except(list2).ToList();

Mono for Android JavaList convert to List and viceversa

How can I convert JavaList to List and vice-versa? I would like to use LINQ, but I cannot do this with JavaList since i cannot cast from anything to JavaList. Any suggestions how can I accomplish this?
You shouldn't have problems switching between the JavaList and List for example in this way:
JavaList<int> a = new JavaList<int>{4, 3, 2, 1};
List<int> b = a.Where(v => v >= 2).ToList();
JavaList<int> c = new JavaList<int>(b);
However it is good to be aware of the consequences, take a look at Chapter 4 in Xamarin API Design.

Categories