C#-无法识别右括号 [英] C# - Closing brackets aren't being recognized
本文介绍了C#-无法识别右括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似这样的控制器方法
I have a controller method that looks like this
public ActionResult SpecialOrderSummary(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
JobOrder jobOrder = db.JobOrders.Find(id);
if (jobOrder == null)
{
return HttpNotFound();
}
ViewBag.JobOrderID = jobOrder.ID;
ItemInstance ii = db.ItemInstances.Where(x => x.serialNumber == jobOrder.serialNumber).FirstOrDefault();
Item item = db.Items.Find(ii.ItemID);
var vm = new ItemViewModel
{
ItemId = item.ItemID,
ItemName = item.Name,
Parts = new List<ItemPartViewModel>
{
foreach (ItemHasParts ihp in item.IHP)
{
Part part = db.Parts.Find(ihp.PartID);
new ItemPartViewModel
{
PartId = part.ID,
PartName = part.Name
};
}
} //this is closing method
}; // this is closing controller
return View(vm);
}
但零件的结尾}
列表和虚拟机不在右括号内。取而代之的是,部件}
关闭方法,而vm }
关闭控制器。
But the closing }
to the Parts list and the vm aren't closing the right brackets. Instead, the Parts }
is closing the method while the vm }
is closing the controller.
为什么会这样?我的语法有问题吗?
Why is this happening? Is there an issue with my syntax?
推荐答案
是的,您不能拥有 foreach $
零件
列表初始化内的c $ c>循环。
Yea you can't have a foreach
loop inside the Parts
list initialization.
您必须执行以下操作:
var vm = new ItemViewModel
{
ItemId = item.ItemID,
ItemName = item.Name,
Parts = new List<ItemPartViewModel>()
};
foreach (ItemHasParts ihp in item.IHP)
{
Part part = db.Parts.Find(ihp.PartID);
vm.Parts.Add(new ItemPartViewModel
{
PartId = part.ID,
PartName = part.Name
};
}
我很惊讶编译器实际上允许您进行编译?
I am surprised the compiler actually let you compile?
这篇关于C#-无法识别右括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文