I'm generating List<int> x
and List<int> y
from List<Point> p
using this code:
List<int> x = (from a in p select a.X).ToList();
List<int> y = (from a in p select a.Y).ToList();
So is there any single LINQ query for getting x
and y
from p
?
No but you can do something like this:
var tuples = p.Select(x => new Tuple<int, int>(x.X, x.Y)).ToList();
But i think that the best solution remains this, using two queries:
List<int> x = (from a in p select a.X).ToList();
List<int> y = (from a in p select a.Y).ToList();
You basically can't, but you can fool yourself:
public static class LinqEx
{
public static void ToLists<T, T1, T2>(this IEnumerable<T> source, SelectorDst<T, T1> selectorDst1, SelectorDst<T, T2> selectorDst2)
{
selectorDst1.List.AddRange(source.Select(selectorDst1.Selector));
selectorDst2.List.AddRange(source.Select(selectorDst2.Selector));
}
}
public class SelectorDst<T, TList>
{
public readonly List<TList> List;
public readonly Func<T, TList> Selector;
public SelectorDst(List<TList> list, Func<T, TList> selector)
{
this.List = list;
this.Selector = selector;
}
}
... Some place in the code
var points = new List<Point>();
var xs = new List<int>();
var ys = new List<int>();
points.ToLists(new SelectorDst<Point, int>(xs, p => p.X),
new SelectorDst<Point, int>(ys, p => p.Y));