我有List
categories
及其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;
}
这些列表与数据表中的记录类似。
答案 0 :(得分:8)
这是我的解决方案,它基于使用queries包。
import 'package:queries/collections.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),
];
var prodTable = new Collection<Product>(products);
print("========================");
print("Map the product list by id");
print("========================");
var result1 = prodTable.toDictionary<int>((product) => product.id);
for (var pair in result1.asIterable()) {
var key = pair.key;
var value = pair.value;
print("$key: $value");
}
print("========================");
print("Group the product list by category");
print("========================");
var result2 = prodTable.groupBy<Category>((product) => product.category);
for (var group in result2.asIterable()) {
var key = group.key;
print(key);
for (var element in group.asIterable()) {
print(" $element");
}
}
print("========================");
print("Create the lookup for product list by category");
print("========================");
var result3 = prodTable.toLookup<Category>((product) => product.category);
for (var category in categories) {
var group = result3[category];
var key = group.key;
print(key);
for (var element in group) {
print(" $element");
}
}
print("========================");
print("Query the aggregate functions");
print("========================");
var result4 = prodTable
.groupBy<Category>((product) => product.category)
.select<Map>((group) => {
"category": group.key,
"products": group.count(),
"max name length":
group.max$1<int>((product) => product.name.length)
});
for (var row in result4.asIterable()) {
print(row);
}
print("========================");
print("Sort the product list by category name and product name");
print("========================");
var result5 = prodTable
.orderBy<String>((product) => product.category.name)
.thenBy<String>((product) => product.name);
for (var product in result5.asIterable()) {
print("${product.category}: $product");
}
}
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;
}
========================
Map the product list by id
========================
0: Dr. Dobb's
1: PC Magazine
2: Macworld
3: Introduction To Expert Systems
4: Compilers: Principles, Techniques, and Tools
========================
Group the product list by category
========================
Magazines
Dr. Dobb's
PC Magazine
Macworld
Books
Introduction To Expert Systems
Compilers: Principles, Techniques, and Tools
========================
Create the lookup for product list by category
========================
Books
Introduction To Expert Systems
Compilers: Principles, Techniques, and Tools
Magazines
Dr. Dobb's
PC Magazine
Macworld
========================
Query the aggregate functions
========================
{category: Magazines, products: 3, max name length: 11}
{category: Books, products: 2, max name length: 44}
========================
Sort the product list by category name and product name
========================
Books: Compilers: Principles, Techniques, and Tools
Books: Introduction To Expert Systems
Magazines: Dr. Dobb's
Magazines: Macworld
Magazines: PC Magazine
答案 1 :(得分:5)
可以使用.map
方法完成映射,如下所示:
products.map((product) => product.id)
这将返回产品ID的列表。
将使用fold方法进行分组。例如,如果您要按类别对产品进行分组:
product.fold<Map<String, List<Product>>>({}, (productMap, currentProduct) {
if (productMap[currentProduct.category.name] == null) {
productMap[currentProduct.category.name] = [];
}
productMap[currentProduct.category.name].add(currentProduct);
return productMap;
});
使用.firstWhere
完成查找。例如:
products.firstWhere((product) => product.category.name === 'books');