强制转换可查询
我如何投射到购物车项目?
var newItem = from i in dc.CartItems
where i.productID == item.productID
select new {productID = i.productID,
Quantity = i.Quantity + item.Quantity ,
cartID = item.cartID };
CartItem itemToUpdate = (here??)newItem;
dc.CartItems.Attach(itemToUpdate, true);
下一个解决方案给了我这个错误>
List<CartItem> newItem = (from i in dc.CartItems
where i.productID == item.productID
select new {productID = i.productID,
Quantity = i.Quantity + item.Quantity ,
cartID = item.cartID }).Cast<CartItem>().ToList();
CartItem itemToUpdate = newItem.First();
dc.CartItems.Attach(itemToUpdate, true);
错误:类型 '<>f__AnonymousType2`3[System.Int32,System.Int32,System.String]' 和 'CartItem' 之间未定义强制运算符。
这段代码怎么样:这也会导致错误错误
if (IfExist(item))
{
//if this product id in cart
// get the quantity of existing cartItem
int quantity = (from i in dc.CartItems
where i.productID == item.productID
select i).First().Quantity;
// sum quanitities of existing and just inserted item
item.Quantity += quantity;
dc.CartItems.Attach(item, true);
:无法添加具有已在使用的密钥的实体。
how do i cast to cartItem?
var newItem = from i in dc.CartItems
where i.productID == item.productID
select new {productID = i.productID,
Quantity = i.Quantity + item.Quantity ,
cartID = item.cartID };
CartItem itemToUpdate = (here??)newItem;
dc.CartItems.Attach(itemToUpdate, true);
next solution gives me this error >
List<CartItem> newItem = (from i in dc.CartItems
where i.productID == item.productID
select new {productID = i.productID,
Quantity = i.Quantity + item.Quantity ,
cartID = item.cartID }).Cast<CartItem>().ToList();
CartItem itemToUpdate = newItem.First();
dc.CartItems.Attach(itemToUpdate, true);
ERROR:No coercion operator is defined between types '<>f__AnonymousType2`3[System.Int32,System.Int32,System.String]' and 'CartItem'.
how about this code : this makes error too
if (IfExist(item))
{
//if this product id in cart
// get the quantity of existing cartItem
int quantity = (from i in dc.CartItems
where i.productID == item.productID
select i).First().Quantity;
// sum quanitities of existing and just inserted item
item.Quantity += quantity;
dc.CartItems.Attach(item, true);
error : Cannot add an entity with a key that is already in use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接转换实际上并不可行 - 您可能希望让 SELECT 创建一个新的 CartItem 而不是匿名类型。
编辑:我的意思是,尝试类似的操作:
假设您的 CartItem 类有一个空的构造函数 - 显然您可以根据需要调整 SELECT。
Direct casting isn't really feasible - you'll probably want to make that SELECT create a new CartItem instead of an anonymous type.
EDIT: By which I mean, try something like:
This is assuming your CartItem class has an empty constructor - obviously you can adapt the SELECT as necessary.