In my DB, I have the following data:
track_id, point_id, latd, lond
A,1,12,100
A,2,13,101
B,1,10,90
B,2,13,90
I am trying to generate a IEnumerable<object[]> as follow:
[[12, 100],[13, 101]], [[10, 90],[13, 90]]
Which will be used in a multilinestring Geojson:
This is what I have tried:
var multiTrackList = _context.tracks
.GroupBy(g => g.track_id)
.Select(s => new object[] {
_context.tracks
.Where(w => w.track_id == s.Key)
.OrderBy(o => o.track_id).ThenBy(o => o.date).ThenBy(o => o.time)
.Select(e => new object[] { e.lond.Value, e.latd.Value }).ToList()
});
but it keeps returning:
[[[12, 100],[13, 101]]], [[[10, 90],[13, 90]]]
with extra unneeded square brackets. I don't see what I am doing wrong. Please help.
CodePudding user response:
I'm not sure what you're trying to accomplish with all the grouping and ordering, but you're doing too many selects on your data source, generating [2][2][2] arrays. Maybe this will point you in the right direction:
context _context = new();
_context.tracks = new();
_context.tracks.Add(new track("A", 12, 100));
_context.tracks.Add(new track("A", 13, 101));
_context.tracks.Add(new track("B", 10, 90));
_context.tracks.Add(new track("B", 13, 90));
var multiTrackList = _context.tracks
.GroupBy(g => g.track_id) //grouping
.SelectMany(e => e.Where(x => x.track_id == e.Key)) //selection where track_id equals Keys on the grouped elements
.OrderBy(o => o.track_id) //ordering, I removed the date and time for simplicity
.Select(y => new object[2] { y.latd, y.lond }) //then you can generate your 2 positions array
.ToList(); //finally you can convert the IEnumerable into a List, or whatever you need
Console.Write(JsonConvert.SerializeObject(multiTrackList)); //"[[12,100],[13,101],[10,90],[13,90]]"
CodePudding user response:
Thanks to @james-braz and after editing his answer, I was able to get the correct linq query:
var multiTrackList = _context.tracks
.GroupBy(g => g.track_id ) //grouping
.Select(e =>
e.Where(x => x.cruise_id == e.Key) //selection where track_id equals Keys on the grouped elements
.OrderBy(o => o.date).ThenBy(o => o.time) //ordering, I removed the date and time for simplicity
.Select(y => new object[2] { y.latd, y.lond })//then you can generate your 2 positions array
).ToList(); //finally you can convert the IEnumerable into a List, or whatever you need
I obtain the correct output ([[12,100],[13,101],[10,90],[13,90]]
), I can use to update the coordinates property of my multilinestring GeoJSON