2
我有categories
的List
及其products
,我想执行这些操作:如何为“列表”执行这些操作:映射,组,查找,聚合,排序?
- 地图
- 集团
- 查找
- 查询聚合函数
- 由多个字段排序
这可以在Dart中完成吗?
void main() {
var books = new Category(0, "Books");
var magazines = new Category(1, "Magazines");
var categories = [books, magazines];
var products = [
new Product(0, "Dr. Dobb's", magazines),
new Product(1, "PC Magazine", magazines),
new Product(2, "Macworld", magazines),
new Product(3, "Introduction To Expert Systems", books),
new Product(4, "Compilers: Principles, Techniques, and Tools", books),
];
// How to map product list by id?
// How to group product list by category?
// How to create lookup for product list by category?
// How to query aggregate functions?
// How to sort product list by category name and product name?
}
class Category {
int id;
String name;
Category(this.id, this.name);
operator ==(other) {
if(other is Category) {
return id == other.id;
}
return false;
}
String toString() => name;
}
class Product {
int id;
String name;
Category category;
Product(this.id, this.name, this.category);
operator ==(other) {
if(other is Product) {
return id == other.id;
}
return false;
}
String toString() => name;
}
这些列表与数据表中的记录类似。
尼斯。这看起来很有用。 –