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.
Related
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.
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).
I have 2 array of object. 1st array of object have property which I want to copy to other array.
1st array of object
HotelRoomResponse[] hr=new HotelRoomResponse[100];
2nd array of object
RateInfos[] rt = new RateInfos[100];
now what i want to do is copy a property of 1st array like
rt=hr[].RateInfo;
but it give error. What is correct way to do this????
You can't just project an array like that. You effectively have to loop - although you don't need to do that manually in your own code. LINQ makes it very easy, for example:
RateInfos[] rt = hr.Select(x => x.RateInfo).ToArray();
Or you could use Array.ConvertAll:
RateInfos[] rt = Array.ConvertAll(hr, x => x.RateInfo);
In both of these cases there's still a loop somewhere - it's just not in your code.
If you're quite new to C# and don't understand LINQ, lambda expressions, delegates etc yet, then you could just write the code yourself:
RateInfos[] rt = new RateInfos[hr.Length];
for (int i = 0; i < rt.Length; i++)
{
rt[i] = hr[i].RateInfo;
}
All of these three will achieve the same result.
The first approach is probably the most idiomatic in modern C#. It will work with any input type, and you can change from ToArray() to ToList() to get a List<RateInfos> instead of an array, etc.
The second approach is slightly more efficient than the first and will work with .NET 2.0 (whereas LINQ was introduced in .NET 3.5) - you'll still need a C# 3 compiler or higher though. It will only work as written with arrays, but there's a similar ConvertAll method for List<T>.
The third approach is the most efficient, but obviously more code as well. It's simpler for a newcomer to understand, but doesn't express what you're trying to achieve as clearly when you know how all the language features work for the first two solutions.
RateInfos[] rt = hr.Select(item => item.RateInfo).ToArray();
Use LINQ:
RateInfos[] rt = hr.Select(x => x.RateInfo).ToArray();
In Perl one can do the following
($a, $b, $c) = split(',', "aaa,bbb,ccc");
does anyone know if there is an equivalent in C# other than doing the following?
var elements = "aaa,bbb,ccc".Split(',');
var a = elements[0];
var b = elements[1];
var c = elements[2];
Or is there an alternative for doing the above more concisely?
No. There's no way of assigning more than one variable in a single assignment expression in C#. Do you definitely need separate variables instead of an array?
Perhaps if you gave us the wider context, we may be able to suggest a better approach to the overall problem - often if you try to approach a task in the way that you would in a different language, you end up with messy code, and that may be the case here.
No there is no other way to do this in C#.
But there is hope in .net - namely F# :D
With this you could do
let [| a; b; c |] = "aaa,bbb,ccc".Split(',')
Still not a perfect solution but with C# 7 we can use tuple deconstruction:
var elements = "aaa,bbb,ccc".Split(',');
var (a, b, c) = (elements[0], elements[1], elements[2]);
I have an array(rows) of arrays which contains 4 elements which I would like to display in a grid or table in a Windows form in C#.
What is the most straightforward way to accomplish this? Is there a way to bind a data grid to an array (or object perhaps)?
What is your recommendation?
You're looking for the DataGridView control.
Instead of an array of arrays, you should make a class with four properties, then make a BindingList<T> of that class.
as SLaks said, that is a more structured way of doing it,
here is a way that uses Linq + Anonymous Types to do something similar on the fly:
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
var evens = from n in numbers select new { Digit = n, Even = (n % 2 == 0) } ;
dataGridView1.DataSource = evens.ToList();
Also if you are after nice ways of displaying then have a look at WPF beats Windows Form hands down.