我遇到了简单的foreach问题。我试图从数据库中获取数据到我的列表。
IList<DeliveredTaskModel> deliveredTaskModel = new List<DeliveredTaskModel>();
// lines of code
if (materialUsed.Count > 0)
{
foreach (var material in materialUsed)
{
var deliveryModel = new DeliveredTaskModel();
deliveryModel.Info = materialUsed[0].SubPartCode;
deliveryModel.Description = materialUsed[0].Description;
deliveryModel.Qty = materialUsed[0].Qty;
deliveredTaskModel.Add(deliveryModel);
}
}
当我在foreach上设置断点时。我可以看到它在materialUsed中有4个不同的项目。但是,当我执行此操作时,它只是将相同项目的4倍添加到网格中。
我认为它会不断添加相同的项目,但为什么呢?有人可以解释一下吗?
答案 0 :(得分:10)
您始终通过索引零访问。要纠正的选项:
如果您使用foreach
,请使用:
foreach (var material in materialUsed)
{
var deliveryModel = new DeliveredTaskModel();
deliveryModel.Info = material.SubPartCode;
deliveryModel.Description = material.Description;
deliveryModel.Qty = material.Qty;
deliveredTaskModel.Add(deliveryModel);
}
如果您使用索引器更改为for-loop
:
for(int i = 0; i < materialUsed.Count, i++)
{
var deliveryModel = new DeliveredTaskModel();
deliveryModel.Info = materialUsed[i].SubPartCode;
deliveryModel.Description = materialUsed[i].Description;
deliveryModel.Qty = materialUsed[i].Qty;
deliveredTaskModel.Add(deliveryModel);
}
然后用户属性初始化程序会更好:
foreach (var material in materialUsed)
{
deliveredTaskModel.Add(new DeliveredTaskModel
{
Info = material.SubPartCode,
Description = material.Description,
Qty = material.Qty
});
}
然后使用linq,您可以使用.Select
var deliveredTaskModel = materialUsed.Select(model => new DeliveredTaskModel
{
Info = material.SubPartCode,
Description = material.Description,
Qty = material.Qty
}).ToList();
我建议你选择最后一个选项:)
最后一条评论 - 你的if语句(materialUsed.Count > 0)
是多余的,因为如果集合为空,它将不会进入循环
答案 1 :(得分:3)
您在循环中引用相同的固定索引:
deliveryModel.Info = materialUsed[0].SubPartCode;
您需要使用循环变量:
deliveryModel.Info = material.SubPartCode;
答案 2 :(得分:2)
您应该在foreach循环的每次迭代中使用当前项而不是引用列表。试试这个:
IList<DeliveredTaskModel> deliveredTaskModel = new List<DeliveredTaskModel>();
if (materialUsed.Count > 0)
{
foreach (var material in materialUsed)
{
var deliveryModel = new DeliveredTaskModel();
deliveryModel.Info = material .SubPartCode;
deliveryModel.Description = material .Description;
deliveryModel.Qty = material .Qty;
deliveredTaskModel.Add(deliveryModel);
}
}
答案 3 :(得分:0)
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($this->_getStoreId($store))
->addAttributeToSelect('name');
if($limit) {
$collection->setOrder('updated_at', 'ASC');
$collection->setPageSize($limit);
}
/** @var $apiHelper Mage_Api_Helper_Data */
$apiHelper = Mage::helper('api');
$filters = $apiHelper->parseFilters($filters, $this->_filtersMap);
try {
foreach ($filters as $field => $value) {
$collection->addFieldToFilter($field, $value);
}
} catch (Mage_Core_Exception $e) {
$this->_fault('filters_invalid', $e->getMessage());
}
$result = array();
foreach ($collection as $product) {
$result[] = array(
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'category_ids' => $product->getCategoryIds(),
'website_ids' => $product->getWebsiteIds(),
'updated_at' => $product->getUpdatedAt(),
'created_at' => $product->getCreatedAt()
);
}
return $result;
materialUsed [0]是你列表中的第一项,无论你在第一项列表中有多少项,你都应该把当前项目当作“材料”