在我的代码中,我有一个ID列表,这些ID在foreach循环中循环显示。
ID与数据库记录有关,我试图将与每个数据库记录有关的项目检索到一个数组中,然后可以用JSON返回
这是我到目前为止所拥有的,但是只返回了属于最后一个ID的项目。
List<int> IDs = new List<int>(bomIds.Split(',').Select(int.Parse));
IEnumerable<BOMItemSummary> bomItemArray = Enumerable.Empty<BOMItemSummary>();
foreach (var value in IDs)
{
BOM bom = db.BOMs.Find(value);
if (bom != null)
{
// Got project, get spec items
bomItemArray = bom.BOMLineItems.Select(bomItem => new BOMItemSummary
{
bomItem = bomItem,
//partNumber = (bomItem.ProductLink.SupplierProductCode != null) ? bomItem.ProductLink.SupplierProductCode : ""
});
}
}
jsonResult = Json(new
{
apiStatus = Utils.Json.JSON_returnStatusSuccess,
//bomTotal = (bom.BOMValue.HasValue ? bom.BOMValue.Value.ToString("0.00") : "0.00"),
bomItemArray = bomItemArray,
}, JsonRequestBehavior.AllowGet);
此后,我需要选择乘积相同的不同值,但要结合数量,因此解决方案应该考虑到这一点
答案 0 :(得分:4)
为什么使用import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class MyInherited extends StatefulWidget {
final Widget child;
MyInherited({this.child});
@override
MyInheritedState createState() => new MyInheritedState();
static MyInheritedState of(BuildContext context) {
return (context.inheritFromWidgetOfExactType(_MyInherited) as _MyInherited).data;
}
}
class MyInheritedState extends State<MyInherited> {
String _myField;
List<String> names;
// only expose a getter to prevent bad usage
String get myField => _myField;
void onMyFieldChange(String newValue) {
setState(() {
_myField = newValue;
});
}
Future<UsersList> fetchUser() async {
final response = await http.get('http://www.json-generator.com/api/json/get/bOEcLwbyqa?indent=2');
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON
return UsersList.fromJson(json.decode(response.body));
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load post');
}
}
@override
Widget build(BuildContext context) {
return new _MyInherited(
data: this,
child: widget.child,
);
}
}
/// Only has MyInheritedState as field.
class _MyInherited extends InheritedWidget {
final MyInheritedState data;
_MyInherited({Key key, this.data, Widget child}) : super(key: key, child: child);
@override
bool updateShouldNotify(_MyInherited old) {
return true;
}
}
class UsersList {
final List<User> users;
UsersList({ this.users, });
factory UsersList.fromJson(List<dynamic> parsedJson) {
List<User> photos = new List<User>();
photos = parsedJson.map((i) => User.fromJson(i)).toList();
return new UsersList(users: photos);
}
}
class User {
String sId;
String name;
String email;
String picture;
String picturep;
String address;
int age;
String gender;
User(
{this.sId,
this.name,
this.email,
this.picture,
this.picturep,
this.address,
this.age,
this.gender});
User.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
name = json['name'];
email = json['email'];
picture = json['picture'];
picturep = json['picturep'];
address = json['address'];
age = json['age'];
gender = json['gender'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['_id'] = this.sId;
data['name'] = this.name;
data['email'] = this.email;
data['picture'] = this.picture;
data['picturep'] = this.picturep;
data['address'] = this.address;
data['age'] = this.age;
data['gender'] = this.gender;
return data;
}
}
?这与IEnumerable<>
无关紧要:
List.AddRange
答案 1 :(得分:1)
您每次都在foreach循环中替换bomItemArray
。您应该使用bomItemArray.Append
向其中添加新项目。
我建议将bomItemArray
的类型更改为List
。
var bomItemArray = new List<BOMItemSummary>();
,然后进入foreach循环
bomItemArray.AddRange(bom.BOMLineItems.Select(bomItem => new BOMItemSummary
{
bomItem = bomItem,
//partNumber = (bomItem.ProductLink.SupplierProductCode != null) ? bomItem.ProductLink.SupplierProductCode : ""
}));
答案 2 :(得分:0)
您可以使用获取代码来提取方法中所需的信息,并使用yield return
获得IEnumerable<YourResult>
。
我认为它将更清洁。