[英]How to get all id in table1 and save to table2 using c# mvc?
I'm a newbie here. 我是这里的新手。 I'm using c# MVC 5.
我正在使用c#MVC 5。
I'll try to get all id with status of "Picked-up" in table1 and save into table2 我将尝试在表1中获取所有状态为“已拾取”的ID并保存到表2中
this is my error. 这是我的错误。
Unable to cast object of type 'WhereSelectListIterator`2[ChenvelIntl.Core.Domain.Packages.Package,System.Int32]' to type 'System.IConvertible'.
无法转换类型为“ WhereSelectListIterator`2 [ChenvelIntl.Core.Domain.Packages.Package,System.Int32]”的类型为“ System.IConvertible”的对象。
This is my code. 这是我的代码。 but But it's not working.
但它不起作用。 Can someone help me on how to solve this?
有人可以帮我解决这个问题吗?
var package = _packageService.GetAll().Where(x => x.Status == "Picked-up");
var shipmentItem = new ShipmentItem
{
ShipmentId = model.ShipmentId
};
if (package != null)
{
shipmentItem.PackageId = Convert.ToInt32(package.Select(x => x.Id));
}
_shipmentItemService.Add(shipmentItem);
return RedirectToAction("addshipmentitem");
Your package
variable would contain a WhereSelectListIterator
or an object that links to MANY packages. 您的
package
变量将包含WhereSelectListIterator
或链接到许多程序包的对象。
Change it to .First()
, eg 将其更改为
.First()
,例如
var package = _packageService.GetAll().First(x => x.Status == "Picked-up");
and then below: 然后在下面:
// If package.Id is not an Int32 already...
shipmentItem.PackageId = Convert.ToInt32(package.Id);
If you need all, change the code like so: 如果您需要全部,请更改代码,如下所示:
var packages = _packageService.GetAll().Where(x => x.Status == "Picked-up").ToArray().Where(x => x != null).ToArray();
foreach(var package in packages) {
var shipmentItem = new ShipmentItem
{
ShipmentId = model.ShipmentId,
PackageId = Convert.ToInt32(package.Id)
};
_shipmentItemService.Add(shipmentItem);
}
return RedirectToAction("addshipmentitem");
just do it : 去做就对了 :
var shipmentItems= _packageService.GetAll()
.Where(x => x.Status == "Picked-up")
.Select(x=>new ShipmentItem(){ShipmentId = x.ShipmentId , PackageId=int.Pars(x.Id)});
if(shipmentItems.Any())
_shipmentItemService.Add(shipmentItems);
return RedirectToAction("addshipmentitem");
if _shipmentItemService.Add() only receives one shipmentItem to add , do this : 如果_shipmentItemService.Add()仅收到要添加的一个shippingItem,请执行以下操作:
shipmentItems.ToList().ForEach(x=>_shipmentItemService.Add(x))
int[] packageIds = _packageService.GetAll().Where(x => x.Status == "Picked-up").Select(x=>x.Id).ToArray();
your will get an int array of packageids then loop throw the array and add your shipment item one by one.. I hope it will work.. 您将获得一个完整的packageids数组,然后循环抛出该数组,并逐一添加您的装运物品。.我希望它能起作用。
foreach (var packageId in packageIds)
{
var shipmentItem = new ShipmentItem
{
ShipmentId = model.ShipmentId,
PackageId = Convert.ToInt32(packageId)
};
_shipmentItemService.Add(shipmentItem);
}
If you would split your functions and change var
into the actual types, you would immediately see your problem: 如果您要拆分功能并将
var
更改为实际类型,您将立即发现问题:
IEnumerable<Package> fetchedPackages = _packageService.GetAll();
IEnumerable<Package> pickedUpPackages = fetchedPackages
.Where(package => package.Status == "Picked-up");
and later: 然后:
IEnumerable<int> pickedUpPackageIds = pickedupPPages
.Select(pickedUpPackage => pickedUpPackage.Id);
shipmentItem.PackageId = Convert.ToInt32(pickedUpPackageIds);
It is easy to see that Convert.ToInt32
does not know how to handle a sequence of pickedUpPackageIds
不难看出,
Convert.ToInt32
不知道如何处理pickedUpPackageIds
序列
What you should change, depends on what you want. 您应该更改什么,取决于您想要什么。
From the identifier name, I get the impression that you expect that there is at utmost one Package with status "Picked-Up": 从标识符名称中,我得到的印象是您期望最多有一个状态为“已拾取”的包装:
int pickedUpPackageId = _packageService.GetAll()
.Where(package => package.Status == "Picked-up")
.Select(package => package.Id)
.FirstOrDefault(); // or SingleOrDefault
shipmentItem.PackageId = Convert.ToInt32(pickedUpPackage.Id);
Note: if there is no such pickedUpPackageId, the value will be zero 注意:如果没有这样的pickedUpPackageId,则该值为零
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.