As example, I have the following Scala class(and the same in C#, ofc):
import com.github.nscala_time.time.Imports._
class Flight{
var FlDate:DateTime = new DateTime
var Origin = ""
var Destination = ""
}
flights = Flight[]
Original C# Code:
var rez = flights.SelectMany(p => new[] {
new { Airport = p.Origin, IsOrigin = true },
new { Airport = p.Destination, IsOrigin = false }
})
.GroupBy(x => x.Airport)
.Select(g => new
{
Airport = g.Key,
LeftCount = g.Count(x => x.IsOrigin),
ArrivedCount = g.Count(x => !x.IsOrigin)
});
Incorrect Scala code:
var rez = flights
.flatMap(case (airport, isOrigin) => {
new { airport = _.origin, isOrigin = true }, // issue - ?
new { airport = _.destination, isOrigin = false } // issue - ?
})
.groupBy(_.airport)
.map {
case (airport, leftCount, arrivedCount) => new {
val airport = ??? // issue - g.Key?
val leftCount = ??? // issue - g.Count(x => x.IsOrigin) ?
val arrivedCount = ??? // issue - g.Count(x => !x.IsOrigin) ?
}
}
So, as you see by the code, as result i need to have an array of objects with the following properties: ListBuffer[airport:String, leftCount :Int, arrivedCount:Int]
So...
- Can someone help to fix the code?
- Maybe, someone knows some good article about "syntax of work with anonymous objects in Scala" topic?