简体   繁体   English

如何使用C#MVC获取table1中的所有ID并保存到table2?

[英]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.

相关问题 如何使用SQL查询删除table1中的某些行及其更改应反映在C#中的table2中? - How to delete some row in table1 and its change should reflect in table2 in C# using sql query? 如何使用Novacode DocX将table1的所有表属性分配给table2? - How to assign all table properties of table1 to table2 using Novacode DocX? 如何使用table1中的字段查找table2并从table2中检索列数据? - How to lookup table2 using a field in table1 and retrieve a column data from table2? C#MVC:功能 <Table1, “runtime type”> 如何获得动态类型? - C# MVC: Func<Table1, “runtime type”> How do I get a dynamic type? 如何使用Linq从table1检索所有列并从table2检索匹配的列(左外部联接) - How to retrieve all columns from table1 and matching columns from table2(Left outer join) using Linq 使用SQL Server使用table2值更新table1 - Update of table1 with table2 values using SQL Server 如何将Table2列添加到table1 - How can I add Table2 columns to table1 如何连接 table1 - table2 和 table1 - table3 并返回 DTO 变量 - how to join table1 - table2 and table1 - table3 and return DTO variable 在table1的指定列之后如何将table2的列连接到table1? - How to join column from table2 to table1 after a specified column of table1? 从表1中选择行,并从表2中选择所有子级到一个对象中 - Select rows from table1 and all the children from table2 into an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM